"no_entries" conditional logic - expressionengine

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

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();

How do you check if a file description is empty or not in Expressionengine 2?

I am using Expressionengine 2. I would like to check to see if the file description is not empty so that if there is any content it will be used instead of the entry title in the alt tag.
Currently I have the code below which brings in the entry title but I can't work out how I get the logic in place to check the existence of descriptive text for the file (image):
<img class="aClass" src="{work_detail_extra_image_01}"
alt="
{if work_detail_extra_image_01:description != ""}
{work_detail_extra_image_01}{description}{/work_detail_extra_image_01}
{if:else}
{title}
{/if}
"
border="0" />
Thanks for taking a look
Try removing != "" so that line becomes:
{if work_detail_extra_image_01:description}.
It'll see if it exists or not.
I'd try to display the description there first without conditionals to make sure they aren't your problem.
Try this:
<img class="aClass" src="{work_detail_extra_image_01}"
alt="{work_detail_extra_image_01}
{if work_detail_extra_image_01:description != ""}
{work_detail_extra_image_01:description}
{if:else}
{title}
{/if}
{/work_detail_extra_image_01}
"
border="0" />

HTMLPurifier allow attributes

I'm having troubles making HTMLPurifier do not filter tag attributes but without success until now and im going crazy.
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8');
$config->set('Core.CollectErrors', true);
$config->set('HTML.TidyLevel', 'medium');
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$config->set('URI.DisableExternalResources', false);
$config->set('HTML.Allowed', 'table[border|width|style],tbody,tr,td,th,img[style|src|alt],span[style],p[style],ul,ol,li,strong,em,sup,sub');
$PHTML = new HTMLPurifier($config);
echo htmlspecialchars($PHTML->purify($html));
// The input string:
"Some <span style="text-decoration: underline;">cool text</span> <img src="http://someurl.com/images/logo.png" alt="" />.
// The output string:
"Some <span>cool text</span> <img src="%5C" alt="" />.
I want to allow the given attributes for specified elements which are defined in HTML.Allowed option.
Turn off magic quotes. (Note the %5C)
Bit of a late suggestion, but I've run into a similar issue with HTMLPurifier stripping style attributes even though they were configured in the HTML.Allowed setting.
The solution I found requires that you also configure CSS.AllowedProperties which looks a bit like this:
$config->set('CSS.AllowedProperties', 'text-align,text-decoration,width,height');
Use this in conjunction with HTML.Allowed:
$config->set('HTML.Allowed', 'img[src|alt|style],span[style]');
I hope someone else finds this useful, you can read more about CSS.AllowedProperties here.

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}`

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

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.

Resources