Is it possible to trigger a conditional in EE via the server time?
{if servertime == 'midnight to 13:00'}
do this
{if:else}
do something else
{/if}
Thank you
Sure, you can use the {current_time} global variable for basic conditionals. To use your example, here's how we'd check that the time's between midnight and 13:00:
{if
'{current_time format="%H%i"}' >= '0000' AND
'{current_time format="%H%i"}' <= '1300'
}
It's between 00:00 and 13:00
{if:else}
It isn't.
{/if}
If you're not opposed to using a little php in your template the user guide has a basic example to get you going: http://expressionengine.com/user_guide/modules/channel/channel_entries.html#start-on
There's also this plugin http://devot-ee.com/add-ons/cc-time-difference which may come in handy.
Just to reiterate what Jean said - if you are doing something like this, you need to make sure your DST (Day Light Saving) is appropriately configured.
If you're not using FocusLab Config - just drop the following into your config.php (in system/expressionengine/config/)
$config['daylight_savings'] = ((bool) date('I')) ? 'y' : 'n';
That'll sort it for you dynamically.
You'll need to watch your DST setup too. One way around this will be to use FocusLab's Master Config which fixes it so DST is handled automatically.
Related
I would like to create custom variable for theos. In example ##DATECREATED## to print current date for my tweak deceptions (I'm soooo bored to edit it manually :D)
Like ##FULLPROJECTNAME## prints out tweak name in control and Makefile...
Edit: I did it with adding this to my nic.pl:
use DateTime;
$NIC->variable("DATECREATED") = DateTime->now->strftime('%d/%m/%Y');
Is it possible to do it without editing original nic.pl?
Thanks for suggestions!
I have found a solution for it!
I had to put this code inside control.pl in my theos template:
use DateTime;
NIC->variable("DATECREATED") = DateTime->now->strftime('%d/%m/%Y');
I've seen someone doing a check on whether an agent's MAC address is on a specific regular expression before it runs the specified stuff below. The example is something like this:
if $is_virtual == "true" and $kernel == "Linux" and $macaddress =~ /^02:00:0A/ {
include nmonitor
include rootsh
include checkmk-agent
include backuppcacc
include onecontext
include sysstatpkg
include ensurekvmsudo
include cronntpdate
}
That's just it in that particular manifest file. Similarly another manifest example but via regular expression below:
node /^mi-cloud-(dev|stg|prd)-host/ {
if $is_virtual == 'false' {
include etchosts
include checkmk-agent
include nmonitor
include rootsh
include sysstatpkg
include cronntpdate
include fstab-ds-dev
}
}
I've been asked of whether can that similar concept be applied upon checking the agent's hostname with a master file of hostnames allowed to be run or otherwise.
I am not sure whether it can be done, but the rough idea goes around something like:
file { 'hostmasterfile.ini'
ensure => present,
source => puppet:///test/hostmaster.ini,
content => $hostname
}
$coname = content
#Usually the start / head of the manifest
if $hostname == $coname {
include <a>
include <b>
}
Note: $fqdn is out of the question.
To my knowledge, I have not seen any such sample manifest that matches the request. Whats more, it goes against a standard practice of keeping things easier to manage and not putting all eggs in a basket.
An ex-colleague of mine claims that idea above is about self-provisioning. However that concept is non-existent in Puppet (he posed that question at a workshop a few months back). I am not sure how true is that though.
If that thing above can be done, any suggestion of how can it be done? Or is it best to go back to the standard one manifest per node for easy maintenance?
Thanks very much.
M
Well, you can replace your node blocks with if constructs.
if $hostname == 'host1' {
# manifest for host1 here
}
You can combine this with some sort of inifile (e.g., using the generate) function. If the <a> and <b> for the include statements are then fetched from your ini file as well, you have constructed a crude ENC.
Note that this has security implications - any agent can claim to have any host name. It's even very simple to do:
FACTER_hostname=kerberos01 puppet agent --test
Any node can receive the catalog for kerberos01 this way. (node blocks rely on $certname instead, which cannot be forged.)
I could not decipher your precise intent from your question, but I suspect that you really want an ENC or a Hiera based approach.
Edit after feedback from your first comment:
To make the master read contents from local files, you should
get rid of the file { 'hostmasterfile.ini': } - it only allows you to set contents, not retrieve them
initialize the variable content using the file function (this will make all nodes fail if the file is not readable)
The code could look like this (assuming that there can be multiple host names in the ini file).
$ini_data = file('/etc/puppet/files/test/hostmaster.ini')
Next step would be a regex lookup like this:
if $ini_data =~ /name=$hostname/ {
Unfortunately, this does not work! Puppet will not expand variable values in regular expressions, apparently.
You can use this (kind of silly) workaround:
$ini_lookup = regsubst($ini_data, "name=$hostname", '__FOUND__')
if $ini_lookup =~ /__FOUND__/ {
...
}
Final remark about security: If your team is adamant about not using $certname for this lookup (although it should be easy to map host names to cert names), you should consider adding the host name to your trusted facts.
I need my [[+idx]] tv to start at 0 instead of 1 so I tried this:
[[+idx:decr]] or [[+idx:substract=1]] but it gives me -1 (minus one).
Does anyone know another way to obtain 0?
Thank you
Using this in chunk for getImageList works (at least for me):
[[+idx:decr]]
It gives: 0,1,2,3 ....
P.S. using modx revo 2.3.1
set your template variable default to 0 when you create the variable.
What are you trying to do, your question is vague at best.
UPDATE
ok - what I think will work for you is to write a snippet to do the math... where ever you call the [[+idx]] instead write a snippet
[[!FixIDX? &itemindex=`[[+idx]]`]]
then in your FixIDX snippet just do the math with php and return the corrected index. Though perhaps a custom output modifier would be the better way to go: http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters+(Output+Modifiers)
Though looking at the docs, your code should certainly work - I see no reason for it not to.
To be honest I'm not expecting an answer to this question. While it sounds simple, I think it's probably quite involved.
For a given channel I want to return one date/time - it will be either the latest channel entry date or the most recent comment date, whichever is the most recent date/time.
Cheers
Lee
Use the Stats Module here to return the required data
http://expressionengine.com/user_guide/modules/statistics/index.html#variables
{exp:stats}
Last Comment Date : {last_comment_date format="%m/%d/%Y %h:%i %a"}
Last Entry Date : {last_entry_date format="%m/%d/%Y %h:%i %a"}
{/exp:stats}
You can try using the IfElse plugin to make sure you don't output the IfElse conditional.
You can use the Statistics Module (as John said) but the following code will output either the comment or entry data like you requested:
{exp:stats}
{if last_comment_date > last_entry_date}
{!-- Comment is more recent --}
{last_comment_date format="..."}
{if:else}
{!-- Entry is more recent --}
{last_entry_date format="..."}
{/if}
{/exp:stats}
I've taken on this EE2 project and I don't have much experience with it.
I've installed two plugins, SessionVariables and Freebies, to fetch a url segment and act according to its value.
So, basically:
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{if:else}
{exp:session_variables:delete name="current_language"}
Hello World!
{/if}
The strange thing is that whan I go to site.com/es/hello it prints "Hola Mundo" but it actually calls both function set() and delete() of session_Variable's plugin.
I'ts driving me crazy, I'd appreciate any help you can provide.
http://www.putyourlightson.net/projects/session_variables
https://github.com/averyvery/Freebie
When using advanced conditionals in EE, all tags within those conitionlas are parsed regardless, as advanced conditionals are parsed later than most other tags in the parse order. This is why the session_variables tag is being called within both conditions in your example (even though only the first bit of content is displayed by the template parser).
The fix is to use "simple conditionals", which are parsed much earlier:
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{/if}
{if "{exp:freebie:any name="es"}" == "false"}
{exp:session_variables:delete name="current_language"}
Hello World!
{/if}
Since this came up pretty early on a Google search I figure I'd mention the add-on IfElse. It'll return the proper condition, the ones that fail (return false) are stripped out. Ergo only the desired condition is run.
As Derek said knowledge of the parse order is vital, in this case both calls to {exp:session_variables:...} are being run regardless of the validity of the conditions. However, with IfElse your code would run as desired while avoiding advanced conditionals so not only do you execute what you want it does it much earlier in the parse order to boot.
E.g.
{exp:ifelse parse="inward"}
{if "{exp:freebie:any name="es"}" == "true"}
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
{if:else}
{exp:session_variables:delete name="current_language"}
Hello World!
{/if}
{/exp:ifelse}
When "{exp:freebie:any name="es"}" == "true" then {exp:session_variables:set name="current_language" value="_es"} will run and not {exp:session_variables:delete...}. As if your code read:
Hola Mundo!
{exp:session_variables:set name="current_language" value="_es"}
Also, again, this will happen at the parse order of {exp:ifelse...}, not after advanced conditionals.
The one caveat to note is that you cannot nest {exp:ifelse...}.
There are probably other add-ons like IfElse (check out Switchee by the same author) for the same or similar contexts, but this is what I know and use.