Skip to content

Instantly share code, notes, and snippets.

@sineld
Created November 2, 2012 08:28
Show Gist options
  • Select an option

  • Save sineld/3999479 to your computer and use it in GitHub Desktop.

Select an option

Save sineld/3999479 to your computer and use it in GitHub Desktop.
useful methods to add to twigview/twigfunctions.php
<?php
//Source: http://forums.laravel.com/viewtopic.php?id=1272&p=2
// useful methods to add to twigview/twigfunctions.php
// return if the current IP matches
function twig_fn_is_ip($ip){
if($ip === $_SERVER['REMOTE_ADDR']){
return true;
}
return false;
}
// convert a timestamp to a string 'x time ago'
function twig_fn_time_ago($timestamp, $precision = 2) {
$time = time() - strtotime($timestamp);
$a = array('decade' => 315576000, 'year' => 31557600, 'month' => 2629800, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'min' => 60, 'sec' => 1);
$i = 0;
foreach($a as $k => $v) {
$$k = floor($time/$v);
if ($$k) $i++;
$time = $i >= $precision ? 0 : $time - $$k * $v;
$s = $$k > 1 ? 's' : '';
$$k = $$k ? $$k.' '.$k.$s.' ' : '';
@$result .= $$k;
}
return $result ? $result.'ago' : '1 sec to go';
}
// return the csrf token
function twig_fn_csrf_token(){
return Session::token();
}
// return an asset as an array of urls
/* {% set assets = {
boostrapper_js: asset_source('boostrapper', 'style'),
bootstrapper_css: asset_source('bootstrapper', 'css')
} %}
{% for asset in assets.bootstrapper_css %}
<link rel="stylesheet" type="text/css" href="{{ asset }}">
{% endfor %}
{% for asset in assets.bootstrapper_js %}
<script type="text/javascript" src="{{ asset }}"></script>
{% endfor %}
*/
function twig_fn_asset_source($container, $type){
$assets = Asset::container($container)->assets;
$sources = array();
foreach($assets as $asset=>$val){
foreach($assets[$asset] as $asset_source => $asset_val){
$src = $asset_val['source'];
$srctype = pathinfo($src, PATHINFO_EXTENSION);
$assetpath = Asset::container($container)->path($src);
if(($srctype == 'css') && ($type == 'style')){
$sources[] = URL::to($assetpath);
}
if(($srctype == 'js') && ($type == 'script')){
$sources[] = URL::to($assetpath);
}
}
}
if(count($sources) > 0){
return $sources;
}
return null;
}
// run the auth check inside templates
// {% if auth_check() %}
function twig_fn_auth_check(){
if(Auth::check()){
return Auth::user()->name; // name not username (consider username private)
}
else{
Auth::logout();
return false;
}
}
// changed to allow passing of arrays, which matches Laravel's method
// {{ url_to('controller@method', [param, param2]) }}
function twig_fn_url_to($route, $params = '')
{
if(is_array($params)){
return URL::to_action($route, $params, false);
}
else if (strlen(trim($params)) == 0)
{
return URL::to_action($route, array(), false);
}
else
{
parse_str($params, $repl);
return URL::to_action($route, $repl, false);
}
}
// allows wordwrap to work through autoescape
function twig_fn_wordwrap($str, $width=40, $cut=true){
return wordwrap($str, $width, chr(13), $cut); // only chr(13) seems to actually work through the escape filter...
}
// again, the regular nl2br filter ends up with the break tags escaped.
function twig_fn_nl2br($str){
$str = str_replace("\r",chr(13),$str);
$str = str_replace("\n", chr(13), $str);
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment