Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Viewed 125 times
3
\$\begingroup\$

I have a Drupal site with roughly the following in my template.php hook_init block:

function mytemplate_init() {
    if (request_path() == "widgets_json") {
        drupal_json_output(_get_widgets());
        exit;
    }
    if (request_path() == "people_json") {
        drupal_json_output(_get_people());
        exit;
    }
}

Are there any problems with this approach?

On the face of it, the two main problems are that it's going to break if URL's ever change, and it's impossible to theme the output of those functions using an appropriate page--widget_json.tpl.php or page--people_json.tpl.php template. On the other hand, adding a content type just for a few JSON output pages that are never going to change paths seems like overkill.

What do you think of this approach?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

From what I see, you could use two different methods: Ternaries and switches:


From a switch point of view, you could use the following to proceed:

function mytemplate_init() {
    switch(request_path()) {
       case "widgets_json":
           drupal_json_output(_get_widgets());
       break;
       case "people_json":
           drupal_json_output(_get_people());
       break;
    }
}

Or, one can replace that with a ternary to shorten it:

if (request_path() == "widgets_json") {
    drupal_json_output(_get_widgets());
    exit;
}
if (request_path() == "people_json") {
    drupal_json_output(_get_people());
    exit;
}

into:

request_path() == "widgets_json" ? drupal_json_output(_get_widgets()) : (request_path() == "people_json" ? drupal_json_output(_get_people()) : "" )
exit;

The shortside to ternaries is that you sacrifice readability for effectiveness.

However, if you can move past that, ternaries are an effective part of a Programmer's Toolbelt(TM)


Or, as I've since come to understand, we can use objects!

$functions = [
       "widgets_json" => _get_widgets,
       "people_json"  => _get_people
     ];
$functions[request_path()]();

Unfortunately, with such a small amount of code supplied, very little can be reviewed.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ "shorter" doesn't always mean "simpler". In this case, I think the original is simpler: you know what's going on with only half eye open. The ternary is very very far from that. \$\endgroup\$
    janos
    –  janos
    2015-06-27 16:17:21 +00:00
    Commented Jun 27, 2015 at 16:17
  • 2
    \$\begingroup\$ Also, readability has been sacrificed as a trade-off. \$\endgroup\$
    Kid Diamond
    –  Kid Diamond
    2015-06-27 17:18:52 +00:00
    Commented Jun 27, 2015 at 17:18
  • \$\begingroup\$ Pretty old post, surprised to see it get an answer - I probably should have specified that I wanted answers from a Drupal perspective, but I thought the "Drupal" tag and the "on the face of it" para should make it fairly clear what my concerns are? I'm aware that I could rewrite the code with a switch or a ternary, but tbh I think both of those options are less clear than the current code. \$\endgroup\$
    George
    –  George
    2015-06-30 05:23:00 +00:00
    Commented Jun 30, 2015 at 5:23
  • \$\begingroup\$ How is that last example valid, unless the _get_widgets and _get_people are constants? They probably should the in quotes. \$\endgroup\$
    kodeart
    –  kodeart
    2015-11-28 18:00:16 +00:00
    Commented Nov 28, 2015 at 18:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.