I just learning Twig templating and I am having trouble figuring out how to load a template part that is in a directory outside of the current themes' directory so they can be reused by other themes.
I am starting with just a simple loading of a head.html template part located in the core/tpls/ directory. With the variouse methodes I have tried, I get the following error:
PHP Fatal error: Uncaught exception 'Twig_Error_Loader' with message
'Unable to find template "/home/ubuntu/workspace/core/tpls/head.html"
(looked into: /home/ubuntu/workspace/themes/core) in "base.html" at
line 5.
The file is in the directory and the path is correct despite that the log error says otherwise.
My base.html located in the themes/core/ directory contains the custom Twig function to include the head.html template located outside of the theme directory:
<html lang="en" class="no-js">
<head>
{% include core.head %} // This is line 5 and throws the error and need to work correctly
// {% include core_head %} // This throws the error and should function the same as {% include core_head %}
// {% include tpl.head %} <-- If I use this, my function for grabbing templates from the theme works since I have a copy of the head.html in the theme dir too //
</head>
<body>
<header id="header">
{% include tpl.header %} // This works via the get_tpl() function
</header>
{% include tpl.breadcrumbs %} // This works too
[. . .]
I hate to clutter this with an entire class but it may be helpful for someone to see what I have wrong. The get_core_tpl() function is what I cannot get working where my other get_*() functions work as they should. See my comments there and also, take note to my comments in the befor_rendor() function:
<?php
class Get_TPLs {
private $tpl_name,
$theme = 'core',
$tpl_array = array(
'breadcrumbs',
'content',
'cover',
'footer',
'head',
'header',
'sidebar'
);
public function before_load_content(&$file) {
$this->tpl_name = basename($file, '.md');
}
public function config_loaded(&$settings) {
if (isset($settings['core_tpl']))
$this->core_tpl = $settings['core_tpl'];
if (isset($settings['tpl_array']))
$this->tpl_array = $settings['tpl_array'];
if (isset($settings['theme']))
$this->theme = THEMES_DIR . $settings['theme'];
}
public function file_meta(&$meta) {
$config = $meta;
if (isset($config['slug'])):
$this->tpl_name = strtolower($config['slug']);
endif;
}
public function before_render(&$twig_vars, &$twig) {
//// ==== THIS IS CALLED WITH {% include core.[TEMPLATE_NAME] %} SEE Base.html ==== ////
// core/tpl/file
$twig_vars['core'] = $this->get_core_tpl();
//// === THIS IS A SIMPLIFIED VERSION OF THE ABOVE VAR - IT'S CALLED WITH {% include core_head %} but It throws the same error ==== ////
$twig_vars['core_head'] = CORE_TPL . '/head.html';
// theme/tpl/file
$twig_vars['tpl'] = $this->get_tpl();
// theme/tpl/views/file
$views = $this->get_views();
$twig_vars['views'] = $views;
// theme/content/file
$theme = $this->get_theme_content();
$twig_vars['theme'] = $theme;
//var_dump($twig_vars['theme']);
}
//// ==== THIS IS THE FUNCTION IN QUESTION ==== ////
private function get_core_tpl() {
foreach ($this->tpl_array as $value) {
$pattern = array('/ /', '/_/');
$name = preg_replace($pattern, '_', $value);
$core[$name] = CORE_TPL . $value . '.html';
}
return $core;
}
private function get_tpl() {
foreach ($this->tpl_array as $value) {
$pattern = array('/ /', '/_/');
$name = preg_replace($pattern, '_', $value);
$tpl[$name] = 'tpl/' . $value . '.html';
$page_tpl = $this->theme . '/tpl/' . $this->tpl_name . '-' . $value . '.html';
if (file_exists($page_tpl))
$tpl[$name] = '/tpl/' . $this->tpl_name . '-' . $value . '.html';
}
return $tpl;
}
private function get_theme_content() {
$content = $this->get_files($this->theme . '/content', '.md');
$content_data = array();
if (empty($content))
return;
foreach ($content as $key) {
$file = $this->theme . '/content/' . $key . '.md';
$pattern = array('/ /', '/-/');
$title = preg_replace($pattern, '_', strtolower($key));
$data = file_get_contents($file);
$content_data[$title] = \Michelf\MarkdownExtra::defaultTransform($data);
}
return $content_data;
}
private function get_views() {
$view_dir = $this->theme . '/tpl/views';
$views = $this->get_files($view_dir);
if (empty($views))
return;
$pattern = array('/ /', '/-/');
foreach ($views as $key) {
$name = preg_replace($pattern, '_', $key);
$view[$name] = 'tpl/views/' . $key . '.html';
if (file_exists($this->theme . '/tpl/' . $this->tpl_name . '-' . $key . '.html'))
$view[$name] = 'tpl/views/' . $this->tpl_name . '-' . $key . '.html';
}
if (!isset($view))
return array(0);
return $view;
}
// scrive.php lib
private function get_files($directory, $ext = '.html') {
if (!is_dir($directory))
return false;
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
$file = $directory . "/" . $file;
if (!$ext || strstr($file, $ext))
$array_items[] = basename($file, $ext);
}
closedir($handle);
}
return $array_items;
}
} ?>
Any help will be greatly appreciated! Thnx
Related
I have a website which already works on 2 languages ,russian and english(everything runs well in both languages), now i have added armenian language.
The Problem --- when i switch on the website into armenain language , i see ,for example,in breadcrumbs
text_home button_continue button_login ....
i have checked \catalog\language\armen\armenian.php file and noticed that values of this varables exist.
By the way ,when i add from armenian.php into ,for example, language/armen/common/header .php this code
$_['text_home'] = 'arm_home';
it works , but thit means that i should add by hand in every single page this general variable...
i would like to have more optimal solution ...
from admin panel i set armenain as default language
Maybe ,i should edit system\library\language.php ???
Here is the structure
<?php
class Language {
private $default = 'en-gb';
private $directory;
private $data = array();
public function __construct($directory = '') {
$this->directory = $directory;
}
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}
public function set($key, $value) {
$this->data[$key] = $value;
}
// Please dont use the below function i'm thinking getting rid of it.
public function all() {
return $this->data;
}
// Please dont use the below function i'm thinking getting rid of it.
public function merge(&$data) {
array_merge($this->data, $data);
}
public function load($filename, &$data = array()) {
$_ = array();
$file = DIR_LANGUAGE . 'english/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . 'english/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->default . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->directory . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$this->data = array_merge($this->data, $_);
return $this->data;
}
}
Thank you in advance
I used an old package of armenain language ,which wasn't compatible with oc 2.3,
solution https://crowdin.com/project/opencart-translation-v2/hy-AM#
I try to parse a ini file to load PHP classes from it but whenever parse_ini_file parses the file, the variable that holds the content is always only "1".
Here is my code:
private $plugins = array();
public function __construct() {
Logger::log("Loading plugins");
Logger::debug("Loading " . APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini");
if (file_exists(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini")) {
$data = parse_ini_file(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini", true);
foreach ($data as $k => $d) {
if (class_exists($k)) {
$plugin = $this->createPlugin($k);
if ($plugin instanceof Plugin) {
$this->init($plugin, $d);
} else {
Logger::error("Plugin doesn't implements interface Plugin");
}
} else {
Logger::error("Can't load plugin $k");
}
}
} else {
Logger::error(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini doesn't exists");
}
}
public function createPlugin($name) {
$plugin = unserialize(
sprintf(
'O:%d:"%s":0:{}', strlen($name), $name
)
);
return $plugin;
}
public function init(Plugin $plugin, $data) {
try {
Logger::log("Adding plugin " . get_class($plugin));
Logger::debug("Calling init");
$plugin->init($data);
$this->plugins[get_class($plugin)] = $plugin;
} catch (PluginException $ex) {
Logger::error("Error while init plugin");
}
}
The ini file:
[\eBot\Plugins\Official\MissionChecker]
url=http://someurl
I tried to add a log message after
foreach ($data as $k => $d) {
But this line was never called, therefore $data must be empty, buw how is that possible?
var_dump and print_r($data) show me "1".
I solved the problem by putting the url in the ini file in quotes.
[\eBot\Plugins\Official\MissionChecker]
url="http://someurl"
I am using codeigniter. I need to get all variables from a language file to an array.Is it possible?
Is there any method available like as follows?
$a = $this->load->language('editor');
print_r($a);
I was tried $this->lang->language; But,This will return labels from another language files loaded.
$CI = & get_instance();
$arr = $CI->lang->language;
Or Use following library
Class My_language {
var $language = array();
/**
* List of loaded language files
*
* #var array
*/
var $is_loaded = array();
function __construct() {
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix == TRUE) {
$langfile = str_replace('_lang.', '', $langfile) . '_lang';
}
$langfile .= '.php';
if (in_array($langfile, $this->is_loaded, TRUE)) {
return;
}
$config = & get_config();
if ($idiom == '') {
$deft_lang = (!isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path . 'language/' . $idiom . '/' . $langfile)) {
include($alt_path . 'language/' . $idiom . '/' . $langfile);
} else {
$found = FALSE;
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) {
if (file_exists($package_path . 'language/' . $idiom . '/' . $langfile)) {
include($package_path . 'language/' . $idiom . '/' . $langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE) {
show_error('Unable to load the requested language file: language/' . $idiom . '/' . $langfile);
}
}
if (!isset($lang)) {
log_message('error', 'Language file contains no data: language/' . $idiom . '/' . $langfile);
return;
}
if ($return == TRUE) {
return $lang;
}
$this->is_loaded[] = $langfile;
$this->language = array();
$this->language = $lang;
return $this->language;
unset($lang);
log_message('debug', 'Language file loaded: language/' . $idiom . '/' . $langfile);
return TRUE;
}
}
Call like this
$this->load->library('my_language');
$arr = $this->my_language->load('demo');
print_r($arr);
I know this is quite an old question, but I just want to give my solution for this problem since no answers has done the trick for this problem. (tested on codeigniter 3)
$this->load->helper('language');
$foo = $this->lang->load('lang_file', 'english', true);
print_r($foo);
notice that the third parameter for load method determines whether to return the loaded array of translations. source: codeigniter 3 docs.
hope this helps
Yeah ofcourse its possible. You can do like this :
//load helper for language
$this->load->helper('language');
//test is the language file in english folder
$this->lang->load('test','english');
//fetch all the data in $var variable
$var=$this->lang->language;
//print $var
print_r($var);
$var will return the array. :)
If you want to return language file data in Array than you need to pass the third parameter in load function.
$this->lang->load('header','hindi',true) // filename,language,true
I can't get this to work, I need to retrieve an CSRF token via an object inside a Session object but it isn't displaying no matter what I try.
I have developed a system that will generate a template from properties from an object, it looks like this:
public function render(){
if(method_exists($this->controller, $this->action)){
//We need to check if the method is used in the controller and not in the parents AppController and/or Controller
$f = new ReflectionClass($this->controller);
$methods = array();
foreach($f->getMethods() as $m){
if($m->name == $this->action){
if($m->class == $this->controller){
$this->controller->{$this->action}();
}
}
}
$this->view_body = $this->render_view();
}else{
$this->error->show('Method ' . $this->action . ' not found in ' . $this->controller, $this->action, $this->controller);
}
$this->_set_render_view();
$this->_set_controller_layout();
if(!file_exists($this->config->paths->root . $this->layouts_path."/".$this->controller->layout)){
$this->error->show('Layout not found', $this->action, $this->controller);
}
if(!file_exists($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view)){
$this->error->show('View not found', $this->action, $this->controller);
}
$layout_content = file_get_contents($this->config->paths->root . $this->layouts_path."/".$this->controller->layout);
$view_content = file_get_contents($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view);
$bind_content = "{% extends \"layout.tmpl\" %} ";
$templates = array();
$bind_content .= '{% block view %}' . $view_content . " {% endblock %}";
$templates['layout.tmpl'] = $layout_content;
foreach($this->controller->snippets as $snippet_name => $snippet){
if(!file_exists($this->config->paths->root.$this->snippets_path ."/" . $snippet)){
$this->error->show('Snippet not found', $this->action, $this->controller);
}
$snippet_content = file_get_contents($this->config->paths->root.$this->snippets_path ."/" . $snippet);
$bind_content .= "{% block ". $snippet_name . " %}" . $snippet_content . " {% endblock %}";
}
$templates[$this->controller->view] = $bind_content;
$loader = new Twig_Loader_Array($templates);
$processwire_vars = array('user', 'pages', 'page', 'sanitizer', 'files', 'input', 'permissions', 'roles', 'templates', 'session', 'config', 'controller', 'wire');
foreach($this->controller->vars as $key=>$val){
$vars[$key] = $val;
}
$twig = new Twig_Environment($loader);
$twig->addGlobal('this', $this);
foreach($processwire_vars as $key => $pw_v){
if(!isset($vars[$pw_v])){
//$vars[$pw_v] = $this->$pw_v;
$twig->addGlobal($pw_v, $pw_v);
}
}
echo $twig->render($this->controller->view, $vars);
}
Now I thought I could access the properties of the session by doing:
{{ session.CSRF.getTokenValue() }}
or
{{ session['CSRF'].getTokenValue() }}
But nothing is displaying.
When I used <?php echo $session->CSRF->getTokenValue(); ?> It worked, so its not the object/method that is failing,
What am I doing wrong here?
EDIT:
It could also be noteworthy that accesing properties like user (user.title, user.name) is possible
<?php
/** Check if environment is development and display errors **/
function setReporting() {
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
}
}
/** Check for Magic Quotes and remove them **/
function stripSlashesDeep($value) {
$value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
return $value;
}
function removeMagicQuotes() {
if ( get_magic_quotes_gpc() ) {
$_GET = stripSlashesDeep($_GET );
$_POST = stripSlashesDeep($_POST );
$_COOKIE = stripSlashesDeep($_COOKIE);
}
}
/** Check register globals and remove them **/
/*function unregisterGlobals() {
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}*/
/** Routing **/
function routeURL($url) {
global $routing;
foreach ( $routing as $pattern => $result ) {
if ( preg_match( $pattern, $url ) ) {
return preg_replace( $pattern, $result, $url );
}
}
return ($url);
}
/** Main Call Function **/
function callHook() {
global $url;
global $default;
global $sent;
$queryString = array();
if (!isset($url)) {
$controller = $default['controller'];
$action = $default['action'];
} else {
$url = routeURL($url);
$urlArray = array();
$urlArray = explode("/",$url);
$controller = $urlArray[0];
array_shift($urlArray);
if (isset($urlArray[0])) {
$action = $urlArray[0];
array_shift($urlArray);
} else {
$action = 'view'; // Default Action
}
$queryString = $urlArray;
if(isset($queryString[0]))
$sent=$queryString[0];
//echo $sent;
}
$controllerName = $controller;
$controller = ucwords($controller);
$model = rtrim($controller, 's');
$controller .= 'Controller';
//echo($model);
//echo($controllerName);
//echo($action);
//echo phpinfo();
**$dispatch = new $controller($model,$controllerName,$action);**
if ((int)method_exists($controller, $action)) {
//call_user_func_array(array($dispatch,"beforeAction"),$queryString);
call_user_func_array(array($dispatch,$action),$queryString);
//call_user_func_array(array($dispatch,"afterAction"),$queryString);
} else {
/* Error Generation Code Here */
}
}
/** Autoload any classes that are required **/
function __autoload($className) {
if (file_exists(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php')) {
include_once(ROOT . DS . 'library' . DS . strtolower($className) . '.class.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php')) {
include_once(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php');
} else {
/* Error Generation Code Here */
}
}
setReporting();
removeMagicQuotes();
//unregisterGlobals();
callHook();
*****//
When I uploaded this file on server it is showing the error
Fatal error: Cannot instantiate non-existent class:updatescontroller on line 97
pointing to the line
$dispatch =new $controller($model,$controllerName,$action);
Please help me determine what is going wrong.
Also the same server is also not allowing me to run the unregisterGlobals() function and showing “too many errors for undefined index”.
The complete project is running very well on my localhost server.
From the error message I guess you are missing an included file that declares the updatescontroller class. Maybe you need to upload the entire project?
It also seems that your local PHP configuration doesn't match the remote configuration. Try matching your local php.ini with the remote server to make local testing more realistic.
If at all possible try to match the PHP version on your local server with the remote server. There can be subtle differences between versions.