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

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" />

Related

conditional xpath statement

This is a piece of HTML from which I'd like to extract information from:
<li>
<p><strong class="more-details-section-header">Provenance</strong></p>
<p>Galerie Max Hetzler, Berlin<br>Acquired from the above by the present owner</p>
</li>
I'd like to have an xpath expression which extracts the content of the 2nd <p> ... </p> depending if there's a sibling before with <p> ... Provenance ... </p>
This is to where I got so far:
if "Provenance" in response.xpath('//strong[#class="more-details-section-header"]/text()').extract():
print("provenance = yes")
But how do I get to Galerie Max Hetzler, Berlin<br>Acquired from the above by the present owner ?
I tried
if "Provenance" in response.xpath('//strong[#class="more-details-section-header"]/text()').extract():
print("provenance = yes ", response.xpath('//strong[#class="more-details-section-header"]/following-sibling::p').extract())
But am getting []
You should use
//p[preceding-sibling::p[1]/strong='Provenance']/text()

awk string search and replace with appended incremented number

I am having some trouble finding string (topic-link) and then appending an incremented number using awk.
I have the following html file that is all one line:
<a class="topic-link" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link" href="test.com/topic/weight-gain">Weight Gain</a> </p>
Using awk I am trying to obtain:
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link2" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link3" href="test.com/topic/weight-gain">Weight Gain</a> </p>
I am running the following:
awk '{gsub("topic-link","topic-link"++i)}1' input file > output file
The problem is that the results will be as follows:
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link1" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link1" href="test.com/topic/weight-gain">Weight Gain</a> </p>
The solution I have come up with will only work correctly if the instances of "topic-link" are on separate lines and as such I am stuck.
Please tell me I am missing something very obvious here or if you have any suggestions for an alternative approach.
Thanks!
Rylan
Your gsub() is only called once so i is only incremented once. You need a loop:
$ awk '{i=0; while(sub(/topic-link"/,"topic-link"++i"\""));} 1' file
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link2" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link3" href="test.com/topic/weight-gain">Weight Gain</a> </p>

Alternating row colors in ExpressionEngine table

I have some code that creates a table that alternates row colors based on the row value of the entry.
<table class="authorList" cellspacing="0">
{exp:channel:entries channel="team" disable="categories|member_data|pagination" orderby="team-last-name" sort="asc"}
{if team-not-with-us != 'y'}
<tr class="{switch="odd|even"} authorInfo">
<th class="authorName">
{if team-bio != ''}<a href="{site_url}about/the-team/{url_title}">{/if}
{title}
{if team-bio != ''}</a>{/if}
</th>
<td class="position">{team-position}</td>
</tr>
{/if}
{/exp:channel:entries}
</table>
The problem is when I delete an entry, I end up having two odd or two even numbers in a row, leaving me with two like-colored rows side by side.
While the switch function is convenient, it is referencing the row count within the database. I don't believe I can apply it to reference the actual row count in the if statement which would solve my problem. (Correct me if I'm wrong.)
I know how to make this change with php:
<?php $oddevenrow = 0; ?>
{if team-not-with-us != 'y'}
<?php $oddevenrow++; ?>
<?php ($oddeven = ($oddevenrow % 2 ? 'odd' : 'even')); ?>
<tr class="<?php echo $oddeven; ?> authorInfo">
But I'm not allowed to turn PHP on in the EE install.
Is there something similar I can do in EE?
Thanks!
You're looking for the switch tag.
{switch="odd|even"}
But it looks like you already knew that. It looks like you're requiring the variable team-not-with-us to != 'y'. Because you're doing that validation after results have been returned you'll end of with multiple odd or even rows next to each other. The easy way to avoid this is to use the channel:entries search param. Example: search:team-not-with-us="not y"
<table class="authorList" cellspacing="0">
{exp:channel:entries
channel="team"
disable="categories|member_data|pagination"
orderby="team-last-name"
sort="asc"
search:team-not-with-us="not y"
}
<tr class="{switch="odd|even"} authorInfo">
<th class="authorName">
{if team-bio != ''}<a href="{site_url}about/the-team/{url_title}">{/if}
{title}
{if team-bio != ''}</a>{/if}
</th>
<td class="position">{team-position}</td>
</tr>
{/exp:channel:entries}
</table>
You might want to try asking the EE peeps at https://expressionengine.stackexchange.com/ The {count} tag should work. That simply counts every entry that (in your case) is in the Team Channel and is not Y in your "team-not-with-us" field group, which I'm assuming is a switch or a select box or something.
What does your outputted code look like?

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.

"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

Resources