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.

Setting Variables Question

March 29, 2009 3:24pm

Subscribe [2]
  • #1 / Mar 29, 2009 3:24pm

    kevincannon

    22 posts

    I’ve got a small problem where I’m displaying a list of directors and their videos. On each video however, I would like to have the something like “Dreams, directed by John Doe” However, since both John Doe and Dreams are both in {title} variables there’s a conflict because they’re nested. Can I store the Title for the main page in a temporary variable and use it later. Here’s what I’m trying at the moment and it’s not working. as intended.

    I think this particular bit might be where I’m making the mistake:
        {assign_variable:dir_name={title}}
    and calling it as
        {dir_name}

    {exp:weblog:entries weblog="directors" dynamic="on" limit="1"}
            {assign_variable:dir_name={title}}
    
            <!-- Director Info -->
               {title}
               {biography}
              
              
            <!-- Director's Videos -->
            {reverse_related_entries weblog="videos"}
                  <a href="http://{video_upload">_              {title} DIRECTED BY: {dir_name} _              </a>
            {/reverse_related_entries}
    
           {/exp:weblog:entries}
  • #2 / Mar 31, 2009 3:48pm

    Shannon Smith

    161 posts

    One thing I see that is definitely not going to work is where you are assigning the variable. In the code above, EE doesn’t know what {title} is unless it is surrounded by the {exp:weblog:entries} tag pair.

  • #3 / Mar 31, 2009 4:32pm

    kevincannon

    22 posts

    Actually, seems I made a typo in the code. I changed it to double check. Original post updated. It still doesn’t work. :(

  • #4 / Mar 31, 2009 4:56pm

    Shannon Smith

    161 posts

    Yeah… may not work to pass a variable in to a related entries tag pair like that.

    One other thing… I think you may be missing some quotes.

    {assign_variable:dir_name="{title}"}

    A good way to make sure that the variable is being assigned properly is to display just the variable you created outside of the related entries tag pair and see if that works…. then it is likely that the problem is trying to use it inside the related entries tag pair.

  • #5 / Mar 31, 2009 5:31pm

    kevincannon

    22 posts

    Cheers for the debugging help. Tried a few things and here’s my results:

    {exp:weblog:entries weblog="directors" dynamic="on" limit="1"}
    {assign_variable:dir_name={title}}
    {title}
    {dir_name}

    BROWSER OUTPUT:
    {assign_variable:dir_name=John Doe}
    {dir_name}

    {exp:weblog:entries weblog="directors" dynamic="on" limit="1"}
            {assign_variable:dir_name="{title}"}
    
            <!-- Director Info -->
               {title}
    
            <!-- Director's Videos -->
            {reverse_related_entries weblog="videos"}
                  {title} DIRECTED BY: {dir_name}
            {/reverse_related_entries}

    BROWSER OUTPUT:
    John Doe
    (director name in title var)
    Video Title DIRECTED BY: Video Title (It seems to be interpreteding dir_name for some reason)

    If there a way to turn on a debugging mode in EE. All this variables malarky is so alien to me compared to other languages.

  • #6 / Mar 31, 2009 5:35pm

    Lisa Wess

    20502 posts

    Assigned variables are the first things parsed in the templates and can not be affected by another tag’s output.  See the docs on assigned variables.

  • #7 / Mar 31, 2009 6:00pm

    kevincannon

    22 posts

    I don’t quite understand that, but I guess it means I can’t do what I want to do.

    Next question, any way to just do this in PHP?

    Could something like this work, or does PHP get parsed first?

    <? $myvar = {title}; ?>
  • #8 / Mar 31, 2009 6:19pm

    Shannon Smith

    161 posts

    Maybe see if an embedded template will do what you are looking for first. Have you used embeds before? They work great when you are having conflicts between tags.

  • #9 / Apr 01, 2009 4:55am

    kevincannon

    22 posts

    That PHP worked thankfully.

    However, I’m still learning the EE lingo, so I may have used embeds already. I’m embedding templates for the top and the header & footer of the page so I can reuse code. Is that the same thing, and if so how does it solve my problem. I’d like to learn to do things the EE way, so I’m not trying to fight the system all the time. 😊

    Thanks!

  • #10 / Apr 01, 2009 11:54am

    Shannon Smith

    161 posts

    Using php is fine, but you should definitely learn to love embeds as there will be times when you have a conflict and a super simple php fix will just not work.

    Basically, you can call an embedded template anytime. Because the two templates are seperate EE parses them differently (embeds are processed after all tags in the originating template are processed) and this generally eliminates conflicts. EE people will often tell you this sort of thing in more complicated language. I’m trying to simplify things, as the EE language often messes me up still as well.

    The other thing you need to get to know is “segments”. What I’m assuming in your example is that one of your URL segments contains the entry id of your “directors” entry. You can pass this entry id into an embedded template and then reuse it there in a weblog entries loop.

    Say segment_3 contains the entry_id. You would include an embed like this where you currently are trying to put title (template_group is whatever makes sense to you and template is a new template named something that will also make sense to you):

    {embed="template_group/template" entry_no="{segment_3}"}

    Then, in the template you created, you would do something like this:

    {exp:weblog:entries entry_id="{embed:entry_no}" dynamic="off" weblog="directors" limit="1"}
    {title}
    {/exp:weblog:entries}

    It’s really important to have all the quotes right and have the embed: before entry_no in the embedded template. This is a lot of work just for one peice, but there will be times when you need to possibly call full extensions or modules and the calls will only work if you embed them in a template like this.

    For more information, check out the docs:
    http://expressionengine.com/docs/templates/embedding_templates.html
    http://expressionengine.com/docs/templates/globals/url_segments.html

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

ExpressionEngine News!

#eecms, #events, #releases