ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to: Add template tags at runtime

May 14, 2008 3:34pm

Subscribe [3]
  • #1 / May 14, 2008 3:34pm

    plank

    30 posts

    Tip

    Hi. This is something I needed the other day, and I thought I would share it.

    How to add template tags at runtime.

    The basic problem was I needed an easily accessible index of cat ids.

    This is for filtering a {exp:weblog} call.

    So, I want to be able to do:

    {exp:weblog:entries category="{cat_id_{segment_1}}"}

    But I don’t want to have to put the cat_id_whatever as global variables through the cpanel.

    I wanted these template tags to be generated from the cat info in the DB. This allows for easy adding of new sections (providing they follow the naming rules).

    My basic template design is to have one site/index.php which then pulls together the various pieces (site/.header, site/.footer…).

    So, at the top of my site/index.php I add:

    global $DB, $TMPL;
    
    //What we're doing here is adding it to the TMPL global vars, so we can access this info like
    //{cat_id_catShortNameHere} 
    //or if we need it at a PHP stage,
    //$TMPL->global_vars['cat_id_catShortNameHere']
    $cats_q = $DB->query("SELECT cat_id, cat_url_title FROM exp_categories");
    foreach ($cats_q->result as $row) {
        $TMPL->global_vars['cat_id_'.$row['cat_url_title']] = $row['cat_id'];
    }

    One thing to note, this has to be in a template that loads before you try to call the tag. You can see the order templates are processed in by turning on template debugging output.

  • #2 / May 14, 2008 4:38pm

    Ingmar

    29245 posts

    That’s nifty. Have you considered writing it up for the Wiki?

  • #3 / May 14, 2008 4:40pm

    plank

    30 posts

    yeah, still new though, so want my little hacks to be reviewed before pushing them there - I often wonder if I overlooked some functionality already built in.

  • #4 / May 14, 2008 4:46pm

    Ingmar

    29245 posts

    Well, the Wiki is well understood to be a community project, so there is certainly no formal review 😊 Feel free to contribute.

  • #5 / May 14, 2008 4:49pm

    nyeoman

    94 posts

    I’ll use this! thanks

  • #6 / May 14, 2008 4:55pm

    plank

    30 posts

  • #7 / Dec 27, 2008 1:05am

    Herb

    224 posts

    I’ve read this post and have read your wiki article.  I did a test just to see if I could make this work.  I can’t.  To simplify the test I just did this

    <?php
    global $TMPL;
    $TMPL->global_vars['copy'] = 'copy';
    ?>

    to avoid complicating thins with a database query.  I then inserted

    {copy}

    further down the template. It is not replaced. Am I doing something wrong?

  • #8 / Dec 27, 2008 7:42am

    Pascal Kriete

    2589 posts

    Re-read the note at the end of plank’s post:

    has to be in a template that loads before you try to call the tag

    So it will work if you embed a template that contains {copy} (you could echo directly from PHP if you need it in the same template).

  • #9 / Dec 27, 2008 2:51pm

    Herb

    224 posts

    Thanks for the clarification Pascal.

    One thing to note, this has to be in a template that loads before you try to call the tag. You can see the order templates are processed in by turning on template debugging output.

    I read it; but, I failed to process the information the way it was intended.  Sometimes the forest obscures the tree.  I can see all kinds of nifty uses for this.

    Thanks to Plank for this great insight and Pascal for hitting me over the head with the obvious.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases