ExpressionEngine - PHP in template, Print something if entry has certain field value? - expressionengine

I have a template that is used for entries. The entries have a field that will always have 1 of 2 values. Can I write some PHP to show something different depending on the field value?
Ive tried the following but it gives me a PHP error:
<?php if($my_field == 'value1') { ?>
<h3>Value 1</h3>
<?php } else { ?>
<h3>Value 2</h3>
<?php } ?>
Thanks

You don't have to use PHP.
{if my_field == 'foo'}
Value 1
{if:else}
Value 2
{/if}
If you're planning on doing any EE tag processing within those conditionals, you shouldn't use the if:else syntax, as with it, the content between the conditionals will always be parsed, but just not displayed, which needlessly uses server resources and increases load time.
So in that case, use simple conditionals instead:
{if my_field == 'foo'}
Value 1
{/if}
{if my_field == 'bar'}
Value 2
{/if}
See: http://expressionengine.com/user_guide/templates/globals/conditionals.html

You can use ExpressionEngine's Conditional Global Variables to display your content, without having to use PHP in your templates.
Rewriting your example using native ExpressionEngine's Simple Conditional tags would result in the following:
{exp:channel:entries channel="channel_name" dynamic="off"}
{if "my_field" == "value1"}
Value 1
{/if}
{if "my_field" == "value2"}
Value 2
{/if}
{/exp:channel:entries}
You can use simple or complex conditionals anywhere in your templates, with the former being less resource expensive, but ExpressionEngine's Parse Order (PDF, 32 KB) may affect how they're evaluated and replaced.
In most cases, you'll need to ensure your custom fields and conditionals are within the {exp:channel:entries} tag loop for the values to be properly output and tested when the page is being built.

Related

jquery / cheerio: how to select multiple elements?

I need to parse some markup similar to this one, from an html page:
<a href="#">
<i class="icon-location"></i>London
</a>
I need to get London.
I did try something like (using cheerio):
$('a', 'i[class="icon-location"]').text();
or
$('a > i[class="icon-location"]').text();
without success...
I'd like to avoid methods like next(), since the expression should be passed to a method which just extracts the text from the selector.
What expression should I use (if it's feasible) ?
There's a solution, which is pretty unusual, but it works :
$("#foo")
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text();
Demo : https://jsfiddle.net/2r19xvep/
Or, you could surround your value by a new tag so you just select it:
<i class="icon-location"></i><span class="whatever">London</span>
Then
$('.whatever').text();
$('a').text();
will get text as 'London'.
$("a .icon-location").map(function(){
return $(this).text()
}).get();

Nested GPath expressions with XmlSlurper and findAll

I'm trying to analyse an XML tree using XmlSlurper and GPath, and the behaviour of the findAll method confuses me.
Say, for example, that you have the following XML tree:
<html>
<body>
<ul>
<li class="odd"><span>Element 1</span></li>
<li class="even"><span>Element 2</span></li>
<li class="odd"><span>Element 3</span></li>
<li class="even"><span>Element 4</span></li>
<li class="odd"><span>Element 5</span></li>
</ul>
</body>
</html>
Assuming that xml has been initialised through one of XmlSlurper's parse methods, the following code executes as one would expect:
// Prints:
// odd
// odd
// odd
xml.body.ul.li.findAll {it.#class == 'odd'}.#class.each {println it.text()}
On the other hand:
// Doesn't print anything.
xml.body.ul.li.findAll {it.#class == 'odd'}.span.each {println it.text()}
I'm struggling to understand why I can use the special # property (as well as others, such as **), but not 'normal' ones.
I've looked at the API code, and what confuses me even more is that the getProperty implementation (found in GPathResult) seems to support what I'm trying to do.
What am I missing?
You need to iterate over every span, so you can use the spread-dot operator:
xml.body.ul.li.findAll {it.#class == 'odd'}*.span.each {println it.text()}

How can I filter search results by category in ExpressionEngine 1.6.8

I have inherited search results template in an EE 1.6.8 install and would like to filter the results by category. This is possible to a point but the paged results are incomplete i.e display an inconsistent no. of results. I believe this is because the template is doing n+1 queries (bad enough in the first place) in a nested query via a custom localisation module (However, if I remove that then my content is not localised)
{exp:search:search_results disable="member_data|trackbacks" orderby"date" paginate="bottom" limit="10" show_expired="no" show_future_entries="no" }
{count}
{exp:localisation:local_query local_sql_url_title="content-from-url-title" page_url_title="{url_title}" }
{categories}
{if category_group ==1}
{if category_name == "Videos"}
<p>
...
</p>
{if:elseif category_name == "audio"}
<p>
...
{if:elseif category_name == "Images"}
...
{if:elseif weblog_short_name == "gpress-releases" }
...
{if:elseif weblog_short_name == "articles" }
...
{if:elseif weblog_short_name == "press-kits" }
...
{/if}
{/if}
{/categories}
{/exp:localisation:local_query }
{/exp:search:search_results}
Does anyone know how I can filter search results by category when an article might be assigned to more than one category (in another group)? I need to display slightly different content e.g. a different icon based on the category name.
Upgrading to EE 2.x is not an option at this time.
Why not just upload your category icon as the category image for each category? Your template will be much cleaner and simpler:
{exp:search:search_results disable="member_data|trackbacks" orderby"date" paginate="bottom" limit="10" show_expired="no" show_future_entries="no"}
{count}
{exp:localisation:local_query local_sql_url_title="content-from-url-title" page_url_title="{url_title}" }
{categories}<img src="{category_image}" alt="{category_name}" class="category-icon" />{/categories}
{/exp:localisation:local_query }
{/exp:search:search_results}`

"no_entries" conditional logic

Is this possible to do the following in ExpressionEngine:
(code taken from here)
IF THERE ARE RELATED ENTRIES SHOW THIS: (important to see the header)
HEADING : Related Entries:
Entry 1
Entry 2
Entry 3
ELSE (SHOW NOTHING)
...
DONE
Code:
{related_entries id="performers"}
{if no_related_entries}
<h2>No Entries</h2> {/if}
<h2>{title}</h2> {body}
{/related_entries}
How do I hide the header? Because the only way to check if there are related entries is to actually start the {related_entries} LOOP.
Any hints? I don't want to hack into PHP for this.
{related_entries id="performers"}
{if title != ""}
<h2>{title}</h2>
{/if}
{body}
{/related_entries}
This should do it, no need for no_related_entries, since you do not plan on doing anything if there is nothing.
Since you have header tags around your title, I imagine you want to avoid printing out header tags when there isn't any related entries.
so if title is not empty, display, if it is, then it won't, so you'll avoid <h2></h2>
don't worry about putting a conditional around the body tag, it will just not display anything if it is blank, but if you put an html tag around it like you did the title, then you would do the same as you do w/ the title conditional.
This ought to do the trick
{related_entries id="performers"}
{if no_related_entries}
<h2>No Entries</h2>
{if:else}
<h2>{title}</h2> {body}
{/if}
{/related_entries}
Sam "SammyTheSnake" Penny

how to access cck checkbox value from views .tpl

Subject for advanced views theming:
1) Create CCK integer field "field_checkbox" - Single on/of checkbox
Allowed values
0|No
1|Yes
2) In views row-style .tpl
<?php print $fields['field_checkbox_value']->content ?>
doesn't print any value, why?
Other fields output fine.
Thank you in advance!
Resolved:
In views settings field output must be unformatted.
Output of above function return 1.
Useful for advanced views theming, for example:
<h3 class="title <?php if ($fields['field_checkbox_value']->content) print 'another-class' ?>">
<?php print $fields['title']->content ?>
</h3>

Resources