Forums

Query Multiple Taxonomies
[resolved] Change list-item.html to list-item.php so I can use PHP (6 posts)

  1. Matt
    Member
    Posted 4 months ago #

    One of my taxonomies is year of birth. I'd like to be able to use a simple PHP function to show the persons (approximate) age next to the year of birth in my list (year of birth doesn't change so can be set as a taxonomy, but age changes so it can't be stored like that), like this:

    2000 - 12 years old
    1999 - 13 years old
    1998 - 14 years old
    and so on...

    The year is the mustache {{name}} variable, the age would be worked out afterwards using a php function.

    However, as the list-item template is html I can't use PHP in it. So, I edited widget.php and changed 'list-item.html' to 'list-item.php' and changed the names of the template files.

    This worked, but now when I try and use PHP in the template my php opening and closing tags are getting converted to comment tags, like this:

    <?php ?>
    <!--? ?-->

    I can see in 'Mustache.php' that it's using RegEx to change stuff, but I don't know much about RegEx.

    Do you know how I could edit 'Mustache.php' to not convert php tags?

    Or, am I going about all this the wrong way, i.e. is there a more simple way to take a mustache variable like {{name}}, check to see if it's a number (a year), and then use some logic on it to work out the approximate age? I don't know if mustache has those kind of features or flexibility?

    Thanks.

    http://wordpress.org/extend/plugins/query-multiple-taxonomies/

  2. scribu
    Member
    Posted 4 months ago #

    The whole idea of Mustache templates is to not mix HTML and PHP, so yeah, you're going about it the wrong way.

    You need to change the 'name' variable before loading the list-item.html template.

  3. Matt
    Member
    Posted 4 months ago #

    Things are usually less complicated than they seem at first ;)

    Any tips on how I might do this, I'm not really sure where to start? Thanks.

  4. Matt
    Member
    Posted 4 months ago #

    Not to worry. I know this is extremely hackish, but I've used Javascript to get the year, work out the approximate age and then append it to the element...

  5. 10sexyapples
    Member
    Posted 3 months ago #

    I'm on the same topic here, so, would like to jump in if I may ... I need to be able to insert an id to the list in order to use the cookie functions for jsTree ( finally getting around to this ) ...

    I have a list of associated custom taxonomy terms in my single template sidebar ... I'm calling that in via a custom walker and have altered the start_el as such to include the ID ...
    $jstreeID = 'cat-item-' . $category->term_id;

    I'm hoping to achieve the same thing with my drilldown lists, but, of course, being confronted with the same problem as Matt.

    I realize that it isn't possible to just change the file type as that's not the mustache that you've loaded into QMT.

    I'm not able to do anything before reaching the template to achieve this ( nothing I can think of anyway ) ... so, I'm trying to determine the best way to go about making this happen.

    I'm thinking that I need to make a new 'stache variable for the ID and get that in there, but, definitely need some wisdom from you on this as I don't want to get hacky~

  6. 10sexyapples
    Member
    Posted 3 months ago #

    Okay, so, here's the only solution I was able to come up with so I could continue taking advantage of the wonderful stashness ...

    I added in a term_id to the $data array -

    `$data = array_merge( $data, array(
    'url' => QMT_URL::for_tax( $this->taxonomy, $tmp ),
    'name' => $term->name,
    'termid' => $term->term_id,
    ) );`

    and then just edited my list-item.html mustache template to include it -

    <li id="{{termid}}" class="term-item {{#is-selected}}current-term{{/is-selected}}">

    Then I added in a little plugin upgrade notification to remind myself not to overwrite my little hack ...

    Thanks for making this so easy Scribu!

Reply

You must log in to post.

About this Plugin

About this Topic