PHP Conditional Statement

I’m using several placeholders [[+placeholder_name]] which are stored as a JSON object. Each one is optional so it may have a value (string) or not (empty). If it does it’s rendered on the front-end as a link.

I’m using a conditional statement to check the placeholder for a value and if empty the front-end field should be left blank, i.e. no link text.

[[!+twitter:isempty =``:else=`<a href="[[+twitter]]” title="[[+twitter]]">Twitter</a>`]]

The problem is that even when the value is empty the link text (e.g. ‘Twitter’) is still rendered which, as far as I can tell, should not happen with the above example. Clearly I’m wrong, though.

Todd
https://xiiro.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I do not see an IF, so how is this conditional?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Are you allowed to use straight PHP in these snippets, or must you encapsulate your code in these pidgin :isempty type statements?

Perhaps the simplest way to do this is to create a function that you can call yourself, so you can use more straightforward code there, and not in your view, which should be “dumb”. Think of this like a Rails helper method.

<?php
function twitter_link($twitter){
  if(empty($twitter)) return '';
  return '<a href="' . $twitter . '" title="' . $twitter . '">Twitter</a>';
}
?>
[[+twitter_link([[+twitter]])]]

Does that make sense?

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Of course I got it sorted 5 minutes after posting last night.

Walter, thank you for the suggestion. The code I posted was by the author’s admission a guess, and not ideal. Straight PHP can and does work just fine except yours didn’t quite do so, though I’m sure it would.

In any case, I’m using a very handy “if” snippet written in what appears(?) to be straight PHP If - If | MODX Documentation, that allows me to write conditionals using a more familiar (albeit regional) syntax.

Here’s the result:

[[!If?
   &subject=`[[!+twitter]]`
   &operator=`empty`
   &then=``
   &else=`<a href="[[!+twitter]]" title="[[!+twitter]]">Twitter</a>`
]]

Alternatively, this one does the same as above but also passes the value (if any) to a filter that returns a list of resources (a tag filter):

[[!If?
	&subject=`[[!+specialties]]`
	&operator=`empty`
	&then=``
	&else=`Specialties:
		<span>
			[[!toLinks?
				&items=`[[+specialties]]`
				&target=`22`
				&tpl=`tagResult`
				&tagKey=`tag`
			]]</span>`
]]

@Ernie - I believe the “isempty" part is essentially the “if” you’re looking for.

Todd
https://xiiro.com

On Mar 11, 2015, at 8:41 AM, waltd email@hidden wrote:

Are you allowed to use straight PHP in these snippets, or must you encapsulate your code in these pidgin :isempty type statements?

Perhaps the simplest way to do this is to create a function that you can call yourself, so you can use more straightforward code there, and not in your view, which should be “dumb”. Think of this like a Rails helper method.

<?php function twitter_link($twitter){ if(empty($twitter)) return ''; return 'Twitter'; } ?>

[[+twitter_link([[+twitter]])]]

Does that make sense?

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options