Is it possible to include a template with {% include %} which is outside from the template path defined with:
$template = $twig->loadTemplate('example.tpl');
I'm asking it because this line doesn't work:
{% include '.../example/navbar_left.tpl' %}
No, it's not possible using Twig_Loader_Filesystem, because it explicitly rejects template names which have .. inside. This can be verified into the definition of function validateName($name) inside file Twig/lib/Twig/Loader/Filesystem.php.
The only clean solution I can think of, if you need to access a template outside the path, is to create your own loader.
A workaround that does work, is defining a symbolic link inside the folder actually registered with Twig_Loader_Filesystem, pointing to the directory you want to access. Be careful with this method, point the link to a safe place.
You can add more paths by following :
https://symfony.com/doc/3.4/templating/namespaced_paths.html
config.yml
# Twig Configuration
twig:
paths:
'%kernel.root_dir%/../src/AppBundle/Resources/views': AppBundle
'%kernel.root_dir%/../web/': 'WEB_DIR_DIST' #your new custom path
someFile.html.twig
<script>{{ source('#WEB_DIR_DIST' ~ 'custom.js') }}</script>
You can add your path to the Twig loader (Filesystem loader)
see last reply from Fabien (addPath method is what you need)
https://github.com/symfony/symfony/issues/1912
you can add the second path to your twig.yaml (symfony 4.4)
twig:
paths:
- '%kernel.project_dir%/templates'
- '%kernel.project_dir%/example'
Then just use the name {% include 'navbar_left.tpl' %}
It is possible through symlink.
Example for Symfony3
from src/AppBundle/Resources/views/Admin
Ubuntu:
ln -s ./../../../../../web/admin/_index-import-body.html index-import-body.html.twig
Windows:
mklink index-import-body.html.twig ./../../../../../web/admin/_index-import-body.html
Then like always include that index-import-body.html.twig in twig file
{% include '#App/Admin/index-import-body.html.twig' %}
Related
As far as I'm aware I'm using best practices to define paths (using raw strings) and how I go about joining them (using os.path.join()), e.g.
import os
fdir = r'C:\Code\...\samples'
fpath = os.path.join(fdir, 'fname.ext')
and doing so has not caused me any problems when running my code within a Python or command shell. If I print fpath to the console I get consistent use of \s in the path:
C:\Code...\samples\fname.ext
But when I run a Docker containerized version of the code and run the image I get the error:
FileNotFoundError: [Errno 2] No such file or directory:
'C:\Code\...\samples/fname.ext'
I don't understand why os.path.join() has used a / to join fdir and fname.ext when the rest of the path included \\. It doesn't do this when I run the code outside of the container.
I have tried using os.path.normpath():
fpath = os.path.join(fdir, 'fname.ext')
fpath = os.path.normpath(fpath)
as discussed here, and os.sep.join():
fpath = os.sep.join([fdir, 'fname.ext'])
as covered here, and Path().joinpath():
from pathlib import Path
fpath = Path(fdir).joinpath('fname.ext')
as well as Path() / 'path_to_add':
fpath = Path(fdir) / 'fname.ext'
as discussed here, but in every case I end up with the same result using os.path.join().
Can someone please help me to understand what is going on and how to create consistent paths that will work whether I run the code in Python in a Windows environment, or in a Docker container?
Update Nov. 16:
In trying to keep my question brief I think I've left out details that are crucial. Apologies to those who have kindly taken the time to offer suggestions based on my incomplete description of the problem.
My code needs to import/export files from/to directories that are defined within a user-specified configuration file.
So the configuration file has a section of code where the user defines variables and paths, e.g.
samplesDir = r"path-to-samples-directory"
The variables are stored in a dictionary of dictionaris and stored as a .json.
At the start of the code the user defines the key that selects the dictionary of interest so that at various parts in my code when a file needs to be imported/exported, the paths are at hand.
So back to my example, samplesDir is stored in the configuration dictionary, cfgDict, so all I need to do is append the file name:
sampleFpath = os.path.join(sampleDir, sampleFname)
and sampleFname is determined based on other variables.
Because of the dynamic nature of the variables (including directory paths and file paths), I think it rules out the use of static path defined in a .yml with Docker Compose.
Update Nov. 18:
It may help to include a few more details and some screenshots.
The above screenshot shows the file and folder structure of the src directory containing the source code, the main app.py script for command-line use, the Dockerfile, etc.
The configs folder contains JSON files that includes variables, paths to directories and files. The user can create configuration files either by copying an existing one and modifying the entries, or configuration files can be generated by calling config.py.
Within config.py I have pre-set variables and paths, so that the directory path to the configuration files (configs), sample files (sample_DROs) and others (e.g. fiducials) are all within src.
I don't anticipate any reason why the user would want to store the config files anywhere else, nor do I expect them to want to use different sample files (or move them elsewhere). However, they will undoubtedly create their own fiducials and may decide not to store them in the fiducials directory (i.e. somewhere not within the src directory).
Likewise I have pre-set the download directory (based on the parameters stored within the configuration files, files are fetched from a server and downloaded) to be the default Downloads directory:
rootDownloadDir = os.path.join(Path.home(), "Downloads", "xnat_downloads")
Those files are later imported, processed, and the outputs are (by default) exported into sub-directories within rootDownloadDir.
Within Dockerfile I set the working directory of the container to be that of the source code and copy all of the contents of src (with the exception of some directories defined in .dockerignore):
WORKDIR C:/Code/WP1.3_multiple_modalities/src
...
COPY . .
so that the structure of the container mimics that of WORKDIR:
Hence I have allowed for flexibility in import/export directories, and they are by default a combination of paths within and outside of the src directory. And so, the code executed within the container will need to access files both within and outside of src.
That said, I don't know what rootDownloadDir will look like when os.path.join(Path.home(), "Downloads", "xnat_downloads") is run within the container.
This has got me thinking - Is it bad practice to set the download directory outside of src?
Returning to the original error:
the sample file is in the container:
From the actual behavior I can suppose that the container is based on Unix-like image. Path separator is / in such systems.
To build an environment-independent path which works inside and outside of the container you need the following steps:
Mounting of host folder to container directory.
Environment variable inside and outside the container.
I can show an example of how this is achievable via docker-compose tool and its configuration file docker-compose.yml:
# docker-compose.yml file
version: '3'
services:
<service_name>: # your service name here
image: <image_name> # name of image your container is built on
environment:
- SAMPLES_PATH=/samples
volumes:
- C:\Code\somepath\samples:/samples
In your python code you can use the following structure:
import os
fdir = os.getenv('SAMPLES_PATH', r'C:\Code\...\samples')
fpath = os.path.join(fdir, 'fname.ext')
I have to configure the linter rules, so that it can detect space in html between "{{ value }}"
for example, this would be ok
<div>{{ value }}</div>
and the error would be wrong
<div>{{value}}</div>
I use: eslint-plugin-vue: 5.2.2
What rule is responsible for it?
I found this rule in eslint-plugin-vue library https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/mustache-interpolation-spacing.md
I'm new to twig. I'm using it in PhpStorm and have problem with paths.
Here is my folder structure:
- template /folder/
- main.twig
- common /folder/
- bootstrap.twig
- nav.twig
In main.twig I have code:
{% extends "common/bootstrap.twig" %}
When I Ctrl + Click on link PhpStorm opens correct file.
But in bootstrap.twig I have to write:
{% include "common/nav.twig" %}
and then Ctrl + Click is not working.
I cannot use relative paths like nav.twig or ./nav.twig - twig throws error.
How can I make twig to use relative paths so go to source would work in PhpStorm?
Mark your template root folder (template in your case) as Resource Root (either via right click in Project View or via Settings/Preferences | Directories).
https://www.jetbrains.com/help/phpstorm/2016.2/configuring-folders-within-a-content-root.html?search=directories
Files under a folder marked as Resource Root can be referenced relative to this folder.
I have a template file (erb) in puppet which is actually the config file of a wordpress installation. The file looks like:
<?php
// DB config
define('DB_NAME', 'wpdb');
define('DB_USER', 'myuser');
define('DB_PASSWORD', 'mypass');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$table_prefix = 'wp_';
..
..
What i wish to achieve is to be able to get the DB_PASSWORD value from trocla. I know that this can be achieved on a manifest file with the following method:
$myvar=trocla('testuser:plain')
However i wish to use trocla on the template erb file. Is this possible?
Something like:
$myvar = '<%= scope.function_trocla(['testuser:plain']).gsub(%r{[\\']}, '\\\\\\0') %>';
See Referencing Custom Functions In Templates in the manual.
I'm trying to compile a Maven project that has a config.properties file. In the file, I have a set of environment variables that I have to set before compile.
In the config.properties file the variables are called like this:
${sys:rdfstore.host}:${sys:rdfstore.port}/openrdf-sesame/repositories/iserve/rdf-graphs/service
How do I have to set the variable rdfstore.host, and to what value should I set it to?
I have tried to solve this with:
export rdfstore.host="localhost"
However, with this I obtain a msj that is a invalid identifier, because
it has a point "." How can I solve this problem?
You should be confusing environment variables and the set of sytem properties:
The properties exported from your system as you did with the export command are called environment variables and should not contain dots in the name.
Those properties are then refered to using ${env.XXX}, meaning in your case you should change the variable name to:
export RDFSTORE_HOST="localhost"
It can then be referred to as below:
`${env.RDFSTORE_HOST}`
System variables are those introcued in command line when invoking a maven phase, those ones can host dots in their names:
mvn -Drdfstore.host="localhost"
They can be referred to as follows:
${rdfstore.host}
You can find more informations in the maven properties manual.