Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hi.

I've been trying all afternoon to figure out what I'm doing wrong.

I've got a template page with a couple of possible variables that get set when a visitor arrives.

Depending on which variable gets set, I'm trying to use a different <cms:pages> tag with different parameters set.

However, when I use a series of <cms:if> statements for each pages option, I get an error:
ERROR! Closing tag "if" has no matching opening tag (line: 165 char: 6403)

Here is my code:
Code: Select all
                <cms:if my_var_1 >
                <cms:pages masterpage='blog.php' limit='20' orderby='random' >
                </cms:if>

                <cms:if my_var_2 >
                <cms:pages masterpage='blog.php' limit='20' orderby='publish_date' paginate='1' >
                </cms:if>

                <cms:if my_var_1=='' && my_var_2=='' >
                <cms:pages masterpage='blog.php' limit='20' paginate='1' custom_field="blog_type=<cms:show blogtype />" >
                </cms:if>


The funny thing is that I'm using exactly the same <cms:if> statements to change the page headings, and they work fine. No errors. Function perfectly.

It's only when I'm using <cms:pages> inside the if statements that I get any error at all.

Any ideas?

If nothing obvious, I'll rebuild the page from scratch and see if that irons things out.

EDIT: It's starting to dawn on me that maybe one can't use pages tags inside if statements, but I don't know where to look to confirm that. Or maybe the problem is that one can't open a pages tag unless one also closes it inside the same if statment. Maybe that's the issue?
Or maybe the problem is that one can't open a pages tag unless one also closes it inside the same if statment.

That is indeed what the problem is. One can only have complete blocks of code nested within the if statements.
Many thanks, KK.

So I can put the complete pages tags, with their enclosed display html, in the page three times -- one per if statment -- which is great.

But first, I wonder if its possible to do something like the following instead, turning my original scheme inside out:

Code: Select all
<cms:pages masterpage='blog.php' <cms:if my_var_1 >limit='20' orderby='random'<cms:else/>
<cms:if my_var_2 >limit='20' orderby='publish_date' paginate='1'</cms:if><cms:else/>
limit='20' orderby='publish_date' paginate='1' custom_field="blog_type=<cms:show blogtype />"</cms:if> >


Trying this, however, I get: ERROR! ATTRIB_NAME: Invalid char "<"
So unless you spot some typo in this code I'll just go ahead with the definitely-works option above.
We cannot add parameters to a tag conditionally (as your code is doing).
What we can do is set *values* of existing parameters conditionally e.g. the 'orderby' param below -
Code: Select all
[code]<cms:pages masterpage='blog.php' limit='20' orderby="<cms:if my_var_1>random</cms:if>">
    ...
</cms:pages>[/code]

Notice in the code above that the 'orderby' parameter is always present but its value is set to 'random' only if a particular condition is met (I hope you recognize the need of using double-quotes as we do above for setting values dynamically; if not please see https://docs.couchcms.com/concepts/sett ... eters.html).

Anyway, directly setting parameters as we did above will soon get too complex for you if the conditions are convoluted (as seems to me in your use-case).

For such cases, you may try using the following method which could be much clearer for you to deal with -
Code: Select all
<cms:set my_orderby="
    <cms:if my_var_1 >
        random
    <cms:else_if my_var_2 />
        publish_date
    <cms:else />

    </cms:if>
"/>

<cms:set my_paginate="
    <cms:if my_var_1 >
       
    <cms:else_if my_var_2 />
        1
    <cms:else />
        1
    </cms:if>
"/>

<cms:set my_custom_field="
    <cms:if my_var_1 >
       
    <cms:else_if my_var_2 />
       
    <cms:else />
        blog_type=<cms:show blogtype />
    </cms:if>
"/>

<cms:pages masterpage='blog.php' limit='20' orderby=my_orderby paginate=my_paginate custom_field=my_custom_field >
    ...
</cms:pages>

Setting a blank value is the same as not using a parameter at all so we can put all the three parameters in the <cms:pages> block knowing that if a condition above sets some parameter to blank, Couch will ignore that param.

Hope this helps.
KK,

Really, thank you so much. All clear now, and the alternative variable-setting is brilliant. I'm going to get to work now.

Very appreciative.
5 posts Page 1 of 1