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.

assigning a custom field to an assign_variable

June 23, 2010 5:46pm

Subscribe [7]
  • #1 / Jun 23, 2010 5:46pm

    ryestew

    19 posts

    In order to avoid nesting weblogs, at the top (or near the top) of a template, I want to grab the data from a few custom fields of a weblog and then use the variables as necessary throughout a page.

    e.g.

    {exp:weblog:entries weblog="{article}" limit="1"}
            {assign_variable:art_pic_1="{article_pic_1}"}
    {/exp:weblog:entries}

    article_pic_1 is the name of a custom field in the article weblog.

    This approach doesn’t work.

    Help please.

    Thanks!

  • #2 / Jun 23, 2010 8:21pm

    Brandon Jones

    5500 posts

    ryestew, have you verified that {article_pic_1} contains the correct value by itself, and have you also tried:

    {assign_variable:art_pic_1 = article_pic_1}
  • #3 / Jun 24, 2010 1:51am

    ryestew

    19 posts

    Oh I’ll give that one a shot.  I got it to work by having a few tags like this on the page

    {exp:weblog:entries weblog="{article}" limit="1"}
            {article_body}"}
    {/exp:weblog:entries}

    and

    {exp:weblog:entries weblog="{article}" limit="1"}
            {article_pic_1}
    {/exp:weblog:entries}

    Is it bad form to have multiple exp:weblog:entries tag on the same page to the same weblog?

  • #4 / Jun 24, 2010 2:37am

    John Henry Donovan

    12339 posts

    ryestew,

    assign_variable is the first thing run in your template as per its parse order so you are never going to be able to assign a variable inside a weblog:entries tag this way.

    Using multiple weblog:entries tags are ok. It just means you are going to have extra queries per page. You can make use of the disable= parameter to limit this query usage though

  • #5 / Jun 26, 2010 3:05pm

    ryestew

    19 posts

    Here’s my latest failure… I tried the assignment without the quotation marks per Brandon’s suggestion but that didn’t work.  So I tried with quotation marks surrounding curly brackets like below:

    {exp:weblog:entries weblog="{article}"  limit="1" url_title="{segment_3}"}
            {article_pic_1}
            {assign_variable:art_pic_1="{article_pic_1}"}
            {art_pic_1}
    {/exp:weblog:entries}
    
    
    {art_pic_1}

    and…
    inside the exp:weblog:entries tag - it {art_pic_1} prints outs the data but outside the exp:weblog:entries tag - it does not work.  So this would seem to be a scope issue.  Any suggestions?

  • #6 / Jun 26, 2010 5:30pm

    Greg Salt

    3988 posts

    Hi ryestew,

    As John stated in his post above, this will not work. You will have a different way of structuring your template. You might also consider Reeposition.

    Cheers

    Greg

  • #7 / Feb 24, 2011 2:58pm

    Peter Kosenko

    13 posts

    So HOW DO YOU CREATE custom variables in Expression Engine Tag language?  It seems like a pretty miserable language that doesn’t allow you to create your own variables.

    Here’s my latest failure… I tried the assignment without the quotation marks per Brandon’s suggestion but that didn’t work.  So I tried with quotation marks surrounding curly brackets like below:

    {exp:weblog:entries weblog="{article}"  limit="1" url_title="{segment_3}"}
            {article_pic_1}
            {assign_variable:art_pic_1="{article_pic_1}"}
            {art_pic_1}
    {/exp:weblog:entries}
    
    
    {art_pic_1}

    and…
    inside the exp:weblog:entries tag - it {art_pic_1} prints outs the data but outside the exp:weblog:entries tag - it does not work.  So this would seem to be a scope issue.  Any suggestions?

  • #8 / Feb 25, 2011 10:05am

    Ingmar

    29245 posts

    So HOW DO YOU CREATE custom variables in Expression Engine Tag language?

    EE supports preload replacements, but they can’t be changed at runtime. Think of them as constants, if you will. Of course you can always use “real” PHP variables, too.

  • #9 / Feb 25, 2011 9:02pm

    ryestew

    19 posts

    I found that the only way to do it was with php variables.  “Preload replacement” variables were too limited.  But php variables did the trick ( and it was a relief to be able to get access to them and to use them too ).

  • #10 / Feb 27, 2011 12:30pm

    Boyink!

    5011 posts

    Couple of things…

    {exp:weblog:entries weblog="{article}"  limit="1" url_title="{segment_3}"}

    Setting the url_title to segment_3 would be redundant as the channel entries tag is being left in dynamic mode and will already see the url title.

    If you want to force-feed a url title parameter like that you also need to dynamic=“no” as a parameter.

    Also if you find you need to get content from the same channel entry multiple times in a template (and there are no other channel:entries tags needed in between) you can run the entries tag as the first and last piece of code in the template and nest all other embeds/html in them. I commonly do this on single-entry templates to avoid multiple channel:entries tags calling the same entry >1 time.

  • #11 / Feb 28, 2011 7:30am

    Sue Crocker

    26054 posts

    Thanks for the assist, Mike.

    ryestew/Peter - any additional questions on this thread?

  • #12 / Mar 09, 2011 7:07pm

    Peter Kosenko

    13 posts

    That’s what I have concluded.

    I found that the only way to do it was with php variables.  “Preload replacement” variables were too limited.  But php variables did the trick ( and it was a relief to be able to get access to them and to use them too ).

  • #13 / Mar 10, 2011 8:31am

    Sue Crocker

    26054 posts

    Sounds good.. closing this thread.. if anyone has any additional questions, feel free to start a new thread based on this one.

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

ExpressionEngine News!

#eecms, #events, #releases