2

I have this code

{if {embed="common/.get-gender-url-title" chosen_gender_id="{exp:stash:get name='chosen_gender_id'}"} != 'male'}

I get this error

Parse error: syntax error, unexpected '{' in .../system/expressionengine/libraries/Functions.php(679) : eval()'d code on line 138

So I tried this

{if "{embed="common/.get-gender-url-title" chosen_gender_id="{exp:stash:get name='chosen_gender_id'}"}" != 'male'}

But I get this error

A PHP Error was encountered
Severity: User Warning
Message: Invalid EE Conditional Variable: {if "embed="common/.get-gender-url-title" chosen_gender_id="2"}
Filename: libraries/Functions.php
Line Number: 2761

What do I need to do to make these code block work?

UPDATE: This is the contents of the embed file

{exp:low_replace find="\r|\n|\t" replace="" regex="yes"}
    {exp:channel:entries
        channel="genders"
        disable="categories|category_fields|member_data|pagination"
        dynamic="no"
        orderby="entry_id"
        sort="asc"
    }
        {if '{embed:chosen_gender_id}' == entry_id}
            {url_title}
        {/if}
    {/exp:channel:entries}
{/exp:low_replace}
  • Can you expand on what the embed is set up to return. Its a parse order issue that you'll have to address but we need to understand what you're testing for. – stuartmcd69 Nov 11 '13 at 3:50
  • @stuartmcd69 Updated question to show the code of embed file – developarvin Nov 11 '13 at 4:03
  • Could you not just use your embed file and use the {if} statement to handle it all ? Instead of {embed:chosen_gender_id} use {exp:stash:get name='chosen_gender_id'} Can you elaborate on what ur trying to achieve ? – stuartmcd69 Nov 11 '13 at 6:53
  • The reason for the embed is to check for the url-title, not the entry_id, since entry_id is volatile. It will change from one installation to another. – developarvin Nov 11 '13 at 7:40
2

Change your quotations:

{if '{embed="common/.get-gender-url-title" chosen_gender_id="{exp:stash:get name='chosen_gender_id'}"}' != 'male'}

UPDATE

Sorry for the crappy first answer. Your issue is, as always, a parse order issue.

Embeds are parsed after conditionals, so the conditional is reading the embed code directly (not parsed) as notacouch mentioned. Since you are using Stash, use it all the way.

Put your embed code into a Stash embed. Then call it in your template where you need to, but after your {exp:stash:set name='chosen_gender_id'}...{/exp:stash:set} tag.

{stash:embed:gender-url-title process="start" parse_stage="set"}

Does that do anything magical?

| improve this answer | |
2

I would avoid using an embed in that case.

An embedded template is the second to last thing to be processed, see ExpressionEngine's Parse Order courtesy Lowe.

If you want to re-use that code you can use it in a snippet (and use an add-on like Brian Litzinger's Snippet Sync to manage snippets as files if that's what's been preventing you from using snippets).

so e.g. your {snippet:getgenderurltitle}'s contents could be like (replace embed with {stash:get...} ):

{exp:low_replace find="\r|\n|\t" replace="" regex="yes"}
    {exp:channel:entries
        channel="genders"
        disable="categories|category_fields|member_data|pagination"
        dynamic="no"
        orderby="entry_id"
        sort="asc"
    }
        {if '{exp:stash:get name="chosen_gender_id"}' == entry_id}
            {url_title}
        {/if}
    {/exp:channel:entries}
{/exp:low_replace}

and stash that, e.g.

{exp:stash:set name="get_gender_url_title" parse_tags="yes" trim="yes"}
  {snippet:getgenderurltitle}
{/exp:stash:set}

Then for your conditional you can use Switchee:

{exp:switchee parse="inward" variable="stash:get_gender_url_title"}
  {case value="male"}
    {!-- do nothing --}
  {/case}
  {case default="Yes"}
    {!-- do something --}
  {/case}
{/exp:switchee}

channel:form comment EDIT

I'm not really familiar with safecracker/channel:form. Can you not use the Query Module for that? E.g.

{exp:query sql="SELECT url_title AS gender_url_title FROM exp_channel_titles WHERE url_title != 'male' AND entry_id = '{exp:stash:chosen_gender_id}'"}
  {gender_url_title}
{/exp:query}

you may not be able to use the no results conditional, in that case stash the results/lack thereof (with parse tags & trim) and use {exp:stash:not_empty ...}

No SQL comment EDIT

You can just stick to embedded templates, then. Except instead of running your conditional in the same template, do it in another one saving any variables you need via stash so you can re-use them in the embedded template.

E.g. your initial embedded template (taking out low replace for stash with trim)

{exp:stash:set name="gender_url_title" parse_tags="yes" parse_conditionals="yes" trim="yes"}
    {exp:channel:entries
        channel="genders"
        entry_id="{embed:chosen_gender_id}"
        limit="1"
        disable="categories|category_fields|member_data|pagination"
        dynamic="no"
        orderby="entry_id"
        sort="asc"
    }
            {url_title}
    {/exp:channel:entries}
{/exp:stash:set}

Then you ought to be able to run your conditional in another embedded template right afterwards (this would be the contents of that embedded template) (untested but it ought to work):

{if {exp:stash:not_empty name='gender_url_title'}
  {if "{exp:stash:get name='gender_url_title'}" != "male"}
    {!-- run code here --}
    ...{exp:stash:get name="saved_channel_form_variable..."}...
  {/if}
{/if}
| improve this answer | |
  • 1
    There is a reason why I can't use snippets, and that is that this code is inside an {exp:channel:form} tag. Because of this, I can't put {exp:channel:entries} inside the form tag because it causes problems. – developarvin Nov 12 '13 at 2:30
  • It looks like you are only looking for chosen_gender_id in channel genders and grabbing the url_title from that. Can you not use the Query Module for that? – notacouch Nov 12 '13 at 21:38
  • @arvinsim couldnt edit comment in time so added to my answer – notacouch Nov 12 '13 at 21:47
  • as much as possible, I don't want to put SQL code in templates. If I have to use SQL, I will simply make a plugin method for it. But the best solution is to avoid these kinds of solution. – developarvin Nov 12 '13 at 23:41
  • Can you explain why? From what it looks like it seems you're looking to compare the url_title of a specific entry_id, so there's no point in looping {exp:channel:entries} nor does it seem you need any of the tag's extra data. I don't get why you'd write a plugin just to run a SQL query, either, unless the chosen_gender_id is something the user can fudge (since it's the only parameter being passed to SQL). – notacouch Nov 13 '13 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.