Created
September 3, 2012 09:35
-
-
Save Rpsl/3608127 to your computer and use it in GitHub Desktop.
Twig filter "String 2 Javascript"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Вывод текста через javascript, чтобы он не индексировался поисковиками. | |
* | |
* @param $str | |
* @return string|bool | |
*/ | |
function str2javascript( $str = FALSE ) | |
{ | |
// <script>document.write('Р'+'о'+'с'+'т'+'о'+'в'+'с'+'к'+'а'+'я'+' '+'о'+'б'+'л'+'.')</script> | |
$req = Request::getInstace(); | |
if( $req->isAjax() ) | |
{ | |
return $str; | |
} | |
if( empty( $str ) || !is_string( $str ) ) | |
{ | |
return FALSE; | |
} | |
$str = preg_replace('~([\s]+)~', ' ', $str ); | |
$js = '<span id="'. md5( $str ).'"></span><script type="text/javascript">'; | |
$js .= '$(document).ready( function(){ $("#'.md5( $str ).'").html(\''. implode("'+'", str_split( $str ) ) .'\'); });'; | |
$js .='</script>'; | |
// document.write не правильно отрабатывает при ajax навигации и history api. | |
#return "<script type='text/javascript'>document.write('" . implode("'+'", preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY) ) . "');</script>"; | |
//return "<script type='text/javascript'>$(document).ready( function(){ document.write('" . implode("'+'", str_split( $str ) ) . "'); });</script>"; | |
return $js; | |
} | |
$twig->addFilter('s2js', new Twig_Filter_Function('str2javascript') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment