MODX Snippet Development VI

In the last article, we changed the &interval property to make the snippet moreMODX logo user-friendly, and we did a little error handling. In this article we’ll look setting placeholders in order to give the user more control over the snippet’s output.

Starting Point

For reference here’s where we left off in the last article:

[code language=”php”]
/* NewResource snippet */

/* Default return value */
$output = ”;

/* Return value for new resources that are published */
$newOutput = $modx->getOption(‘showNew’, $scriptProperties,
‘<span class="new_resource">NEW!</span>’, true);

/* Set $interval as the number of seconds in two weeks */
$interval = $modx->getOption(‘interval’, $scriptProperties, ‘2 weeks’, true);

$cutoffTime = strtotime(‘now – ‘ . $interval);

if ($cutoffTime === false) {
$output = ‘[NewResource] Invalid interval’;
}

/* See if the resource is published */
$published = $modx->resource->get(‘published’);

/* See if publication date is within the last 2 weeks */
if ($published && $cutoffTime) {
/* Get the current time as a timestamp */
$currentTime = time();

/* Get the publishedon date and
convert it to a unix timestamp */
$ptime = strtotime($modx->resource->get(‘publishedon’));

if ($ptime > $cutoffTime) {
/* Yes, it’s recent — Set the return value */
$output = $newOutput;
}
}
}

return $output;
[/code]

A Feature Request

Imagine that you have a user who wants to display the snippet’s output in several places on the same page. It would be wasteful to have the snippet run and do all the calculations for each location, since it only has to happen once. This is a perfect situation for placeholders. We’ll create the option to set a placeholder with the snippet’s output. The the user can put that placeholder anywhere on the page and use it in as many places as necessary.

We’ll use a property called &ph. We’ll let the user give us the name of the placeholder and we’ll only set it if it has a value. As long as the NewResource snippet tag is above any placeholders on the page, they’ll all be set:

[code language=”html”]
[[!NewResource? &showNew=`NEW!!!` &ph=`MyPlaceholder]]

<p>This page is [[+MyPlaceholder]] [[+MyPlaceholder]] [[+MyPlaceholder]] </p>
[/code]

The three placeholders in the HTML above will be replaced by the &showNew value. If we wanted the message to be a little more eye-catching, we could do this:

[code language=”html”]
[[!NewResource? &showNew=`<span style="color:red;">NEW!!!</span>` &ph=`MyPlaceholder]]
[/code]

In keeping with our previous principle of not mixing style and content, though, this would be a better approach:

[code language=”html”]
[[!NewResource? &showNew=`<span class="new_resource">NEW!!!</span>` &ph=`MyPlaceholder]]
[/code]

Our CSS file could then contain this line, which could be changed later to alter the color of all messages on all pages:

[code language=”html”]
.new_resource {
color:red;
}
[/code]

You might be thinking that the span tags should go in the page rather than the snippet, since they are part of the view, not the business logic. The problem with this is that the span tags will then be part of the page regardless of the page’s age and whether the user has set a placeholder or not. You can eliminate them with an output modifier that only displays them when they are not empty, but that’s what our snippet does, and it will do it much faster than the output modifier would. In the interest of faster page-load times, putting them in the snippet is probably justified.

Setting the Placeholder

To make the placeholder work, we’ll add this code near the top of the snippet to get the &ph property:

[code language=”php”]
$ph = $modx->getOption(‘ph’, $scriptProperties, ”, true);
[/code]

If the user doesn’t send that property, or if the property is empty, the $ph variable will be empty.

We also don’t want to return the output (which would be displayed at the location of the snippet tag), if the user has specified a placeholder. We’ll change the return statement at the end accordingly:

[code language=”php”]
if (!empty($ph)) {
$modx->setPlaceholder($ph, $output);
$output = ”;
}

return $output;
[/code]

This way, if the user sends the &ph property, the placeholder will be set and nothing will appear at the location of the snippet. If the &ph property is empty or not sent, the output will appear where the snippet tag is placed.

It’s important to always put a snippet tag above any placeholders it sets. Otherwise, the placeholders won’t be set at the time they are parsed and they will be removed.

New Code

Here’s the final version of the code with the placeholder option:

[code language=”php”]
/* NewResource snippet */

/* Default return value */
$output = ”;

/* Optional placeholder */
$ph = $modx->getOption(‘ph’, $scriptProperties, ”, true);

/* Return value for new resources that are published */
$newOutput = $modx->getOption(‘showNew’, $scriptProperties,
‘<span class="new_resource">NEW!</span>’, true);

/* Set $interval as the number of seconds in two weeks */
$interval = $modx->getOption(‘interval’, $scriptProperties, ‘2 weeks’, true);

$cutoffTime = strtotime(‘now – ‘ . $interval);

if ($cutoffTime === false) {
$output = ‘[NewResource] Invalid interval’;
}

/* See if the resource is published */
$published = $modx->resource->get(‘published’);

/* See if publication date is within the last two weeks */
if ($published && $cutoffTime) {
/* Get the current time as a timestamp */
$currentTime = time();

/* Get the publishedon date and
convert it to a unix timestamp */
$ptime = strtotime($modx->resource->get(‘publishedon’));

if ($ptime > $cutoffTime) {
/* Yes, it’s recent — Set the return value */
$output = $newOutput;
}
}
}

if (!empty($ph)) {
$modx->setPlaceholder($ph, $output);
$output = ”;
}

return $output;
[/code]

Coming Up

In this article, we looked at using a placeholder to give the user more control over where the output appears. In the next one, we’ll see how to internationalize our snippet so it can be used in more than one language.

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than <a
href=”http://bit.ly/YgFGHl”>Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *