1

I have several calls to a getContent() function with arguments that are pulled into the function using func_get_args(). Several of the arguments stay the same, and I would like to set them as a variable to prevent having to duplicate the repeated arguments throughout all of these function calls.

Here is an example of the function call with the full arguments:

<? getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:pastors-team",
    "order:series",
    "find_parent_category:team",
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>"
); 
?>

Ideally, I would be able to do something like this:

<? $myStyle = 
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>";

getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:leadership-team",
    "order:series",
    "find_parent_category:team",
    $myStyle
);

getContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:executive-team",
    "order:series",
    "find_parent_category:team",
    $myStyle
); ?>

Is this possible? What is the best way to approach this?

Note: I cannot change the getContent() function, it's a core function of the CMS

4 Answers 4

1

This is the simplest one i can think of:

call_user_func_array('getContent',array_merge(array(
    "article",
    "display:list",
    "find_series:team",
    "find_category:executive-team",
    "order:series",
    "find_parent_category:team",
    ),$myStyle)
);

Edit: Yazmat's solution will probably be better.
You can use it with a variable number of arguments like this:

<?php
function myGetContent() {
    $defaults = array(
        "show:<div class='box box_stories'>",
        "show:<div class='box_pad box_img bwWrapper'>",
        "show:<div class='box_inner_pad'>",
        "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
        "show:<p class='teamsummary'>__summary__</p>",
        "show:<p class='phone_icon'>__authorXworkphone__</p>",
        "show:</div>",
        "show:</div>",
        "show:</div>"
    );
    return call_user_func_array('getContent',array_merge(func_get_args(), $defaults));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was able to run it like this with a dummy getContent function (which just var_dumps its input). Are you getting any errors, warnings.. or the function is just doing nothing ?
1

Unfortunately, this is not possible. What you have now is passing article, display, etc and $myStyle (which should be an array). You'll get errors because you didn't pass enough arguments.

If you can, check and see if getContent() has default parameters.

Comments

1

You can create you own function that only takes the arguments you need and that calls the getContent() function with those arguments plus the defaults one. and use this new function instead of getContent()

Edit :

You do something like this :

<?php
function myGetContent($param1,$param2,$param3,$param4,$param5,$param6){
  return getContent(
    $param1,
    $param2,
    $param3,
    $param4,
    $param5,
    $param6,
    "show:<div class='box box_stories'>",
    "show:<div class='box_pad box_img bwWrapper'>",
    "show:<div class='box_inner_pad'>",
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>",
    "show:<p class='teamsummary'>__summary__</p>",
    "show:<p class='phone_icon'>__authorXworkphone__</p>",
    "show:</div>",
    "show:</div>",
    "show:</div>"
  );
}

and then you call :

myGetContent(
    "article",
    "display:list",
    "find_series:team",
    "find_category:leadership-team",
    "order:series",
    "find_parent_category:team"
);

2 Comments

Can you elaborate a little further? I'm not sure exactly how I would pass the standard arguments along to the getContent() function? Seems like I'm facing the same issue.
@Yazmat you might want to generalize that with func_get_args(). Someone might copy it like this :P
0

you need this?

<?php

getContent(
    array(
        'type'                  => 'article',
        'display'               => 'list',
        'find_series'           => 'team',
        'find_category'         => 'leadership-team',
        'order'                 => 'series',
        'find_parent_category'  => 'team'
    ),
    array(
        '<div class="box box_stories">',
        '<div class="box_pad box_img bwWrapper">',
        '<div class="box_inner_pad">',
        '<h3><a href="/staff-detail/__slug__">__authorX__</a></h3>',
        '<p class="teamsummary">__summary__</p>',
        '<p class="phone_icon">__authorXworkphone__</p>',
        '</div>',
        '</div>',
        '</div>'
    )
);


function getContent($argv, $shows){
    echo '<p>'.$argv['type'].'</p>';
    echo '<p>'.$argv['list'].'</p>';
    /* ... etc ... */

    echo '<pre>'.htmlspecialchars(print_r($argv, true), ENT_QUOTES).'</pre>';
    echo '<pre>'.htmlspecialchars(print_r($shows, true), ENT_QUOTES).'</pre>';
}

1 Comment

Unfortunately I can't change the getContent function itself. It's a core part of the CMS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.