|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Is this useful? |
Yes |
|
33% |
[ 1 ] |
Yes - but it's not for me |
|
0% |
[ 0 ] |
No |
|
66% |
[ 2 ] |
|
Total Votes : 3 |
|
Author |
Message |
wern0561 Smarty Rookie
Joined: 05 Jan 2009 Posts: 13
|
Posted: Wed Feb 23, 2011 10:17 pm Post subject: Display part of a template in a given time range |
|
|
Sometimes I find the need to display some content on a page only during a very specific time frame. Usually something like this is handled at the application level not the presentation level but its not always worth the time or investment to create this for a very simple posting. This isn't meant to replace your cms just give you an easy way time post/remove simple time critical information.
This has a number of potential applications: time sensitive announcements, promotional events, maybe you need that shamrock image to show up on your homepage on St. Patrick's day while your out of town, or maybe you just want to go on vacation and make sure things are added and taken off your site when they should be.
Let me know if you have any suggestions for improvement. Here's the code:
Code: |
<?php
/*
* Smarty {display_time_range}{/display_time_range} block plugin
* -------------------------------------------------------------
* File: block.display_time_range.php
* Type: block
* Name: display_time_range
* Purpose: display the contents of the block during a specified time range
* @param array
* <pre>
* Params: start: string(ten seconds from now)
* end: string(ten seconds ago)
* show_erros: boolean (false)
* </pre>
* Examples:
* <pre>
* {display_time_range end='2012-3-23'}Hello world{/display_time_range}
* {display_time_range start='2010-3-23'}Hello world{/display_time_range}
* {display_time_range start='2010-3-23' end='2012-3-23'}Hello world{/display_time_range}
* {display_time_range start='Feb 27 2011' end='Mar 10 2012'}Hello world{/display_time_range}
* {display_time_range end='2012-3-23 12:30:30'}Hello world{/display_time_range}
* </pre>
* Caution: Avoid using relative times. They should parse correctly but will never or always display the content.
* <pre>
* {display_time_range end='tomorrow'}This will always display{/display_time_range}
* {display_time_range start='tomorrow'}This will never dispay{/display_time_range}
* </pre>
* Note: Both the start and end paramaters are optional but at least one is required.
* @author Zach Werner
* @param string contents of the block
* @param Smarty clever simulation of a method
* @return string $content or nothing
* -------------------------------------------------------------
*/
function smarty_block_display_time_range($params, $content, &$smarty, &$repeat)
{
if (is_null($content) || $repeat) {
return;
}
$start;
$end;
$show_errors = false;
foreach ($params as $_key => $_val) {
switch ($_key) {
case 'start':
case 'end':
$time = strtotime($_val);
if($time === false) {
if($show_errors) {
$smarty->trigger_error("date format: invalid date $_key = '$_val'");
} else {
return;
}
} else {
$$_key = $time;
}
break;
case 'show_errors':
$$_key = (bool)$_val;
break;
default:
$smarty->trigger_error("textformat: unknown attribute '$_key'");
}
}
if(!isset($end) && !isset($start)) {
if($show_errors) {
$smarty->trigger_error("A start date or an end dates are required for the use of the display_time_range block plugin");
} else {
return;
}
} elseif(!isset($start)) {
$start=time() - 10;
} elseif(!isset($end)) {
$end=time() + 10;
}
$now = time();
if($now > $end || $now < $start) {
return;
} else {
return $content;
}
}
?>
|
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|