Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a custom template variable in expressionengine

I need to output the category for an entry a few times in an entry's template.

So I want to get the output from the following and assign to a variable so I can reuse within the template:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}{category_name}{/categories}
{/exp:channel:entries}"

How do it do that?

like image 782
onblur Avatar asked Sep 25 '11 23:09

onblur


2 Answers

Now, you could enable the template to allow PHP, then you could write something like this:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}
        <?php $category = '{category_name}'; ?>
    {/categories}
{/exp:channel:entries}

Then you have the {category_name} stored in the php-variable "category". Later you can reuse it as you wish, like echoing it:

<?php echo $category; ?>

You can even compare it to other EE-tags:

{exp:channel:entries channel="product" limit="1" status="open"}
    {if <?php $echo($category) ?> == title}
        This title have got the same value as the category!
    {/if}
{/exp:channel:entries}
like image 58
Marcus Olsson Avatar answered Sep 25 '22 10:09

Marcus Olsson


Croxton's Stash: http://devot-ee.com/add-ons/stash does very nearly the same thing NSM Transplant (mentioned by Derek, above) does, and is free. One of these addons would definitely be the easiest way to do what you're trying to do.

like image 32
adrienne Avatar answered Sep 24 '22 10:09

adrienne