Relative paths in twig or absolute in PhpStorm - twig

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.

Related

How do I exclude a folder from the sidebar in Sublime Text permanently, specifying it relative to the open folder?

I've already read this related question (How do I exclude a folder from search in sublime text 3 permanently?) but my question is different since I want to specify only the folder at the open folder's root, not a generic pattern to match at any level in the folder tree.
In Sublime Text 4 I have an open project folder via File --> "Open Folder...".
Let's say my folder layout is this:
mainapp
├── microapp
│ └── node_modules <== don't exclude this (keep it)
├── microapp2
│ └── node_modules <== don't exclude this (keep it)
├── index
├── node_modules <=== exclude this only
├── config
└── assets
I'd like to exclude mainapp/node_modules only, NOT mainapp/microapp/node_modules nor mainapp/microapp2/node_modules. How do I do that?
I'm guessing I need to specify a "folder_exclude_patterns" in the settings.
Side note: why do I need to do this?
Because that folder has so much build content in it (which is constantly-changing as builds occur) that it's actually causing Sublime Text to freeze and lock up and become unusable.
Tested on Linux Ubuntu 18.04.
Through sheer dumb luck and persistence with guessing, I figured it out. // refers to the "open folder root", apparently.
If you want to see this info about // added to the official Sublime Text documentation and default settings file, please upvote my open issue on it here.
Update
I found some official documentation on this: https://www.sublimetext.com/docs/file_patterns.html. The // feature was added as of Sublime Text 4:
If pattern begins with //, it will be compared as a relative path from the project root [added in version 4.0]
My testing, however, proves that the // actually means "path" root, as defined below, however. So, my examples below are still correct.
1. If you have a folder open via File --> "Open Folder...", do this:
Preferences --> Settings --> add this "folder_exclude_patterns" entry to your user settings JSON file:
{
// other user settings here
// exclude only the "mainapp/node_modules" dir
"folder_exclude_patterns": ["//node_modules"],
// other user settings here
}
Again, // means the "open folder's root".
NOTE: Changing your user settings above will apply globally to all of your Sublime Text instances, which may not be what you want. So, it may be better to use a "Project" instead, as described below:
2. If you have the folder open and saved as part of a project, do this:
Project --> Edit Project --> add this "folder_exclude_patterns" entry to your Project settings JSON file:
{
"folders":
[
{
// path to an open folder in a project
"path": "/path/to/mainapp",
// exclude only the "mainapp/node_modules" dir
"folder_exclude_patterns": ["//node_modules"],
}
],
}
You can see in the official project settings file example here (https://www.sublimetext.com/docs/projects.html) that the "folder_exclude_patterns" entry must be at the same level in the JSON settings file as the "path" entry.
I also first learned this from #smhg's comment here. Thank you!
To open another folder in your project, go to Project --> "Add Folder to Project...". Once you have multiple folders open in your project, you'll have to add multiple entries of "folder_exclude_patterns", as desired, like this:
{
"folders":
[
{
// **absolute path** to open a folder in a project
"path": "/path/to/mainapp",
// exclude only the "mainapp/node_modules" dir
"folder_exclude_patterns": ["//node_modules"],
},
{
// or **relative path** to open another folder in the project;
// the path is relative to the location of the
// "project_name.sublime-project" project file itself
"path": "some_dir",
// exclude only the "some_dir/path/to/excluded_folder" dir
"folder_exclude_patterns": ["//path/to/excluded_folder"],
},
],
}
Bonus: How to create a project in Sublime Text:
To create a Project from an open folder, the steps are like this:
Open a folder: File --> "Open Folder..."
Save it as part of a project: Project --> "Save Project As..."
Now you can choose where to save your project_name.sublime-project file. This is the file you are editing when you go to Project --> "Edit Project" above. To open a project go to Project --> "Open Project...".
See also:
Issue I opened: https://github.com/sublimehq/sublime_text/issues/5234
Comment I wrote on the Sublime Text forum: https://forum.sublimetext.com/t/a-way-to-specify-root-in-project-settings/7756/4?u=ercaguy
Official Project settings documentation: https://www.sublimetext.com/docs/projects.html. This includes:
Each object must have a "path" key, which may be relative to the project directory, or a fully qualified path.
How do I exclude a folder from search in sublime text 3 permanently? - answer which explains how to exclude a file or folder from the side bar in Sublime Text, versus excluding a file or folder from search, such as Goto Anything or Find in Files.

Getting "No file or variants found for asset" found error on my asset

I am getting this attached error when I try and run my app. Any ideas?
(Below)
Launching lib\main.dart on SM J200Y in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\apk\debug\app-debug.apk.
Error detected in pubspec.yaml:
No file or variants found for asset: assets/credentials.json.
I have checked the yaml file and the formatting seems to be fine. I am at a loss as want the issue is. (Also, below)
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/credentials.json
In my case, the problem was that I was missing slash /.
At first, I have added my icons path as below.
assets:
- assets/icons
I solve the issue by adding as slash / to the end of the path as below:
assets:
- assets/icons/
Check identation of assets. Generally it should have 3 spaces from start of new line.
assets:
- assets/credentials.json // <-- count three spaces from start
I was facing the same problem, and I had two mistakes:
I created the "images" folder inside the "lib" folder, and it should be outside the lib folder, it should be in the app root folder.
It should be three spaces from the starter line as #Darshan said
-assets/credentials.json // <-- count three spaces from start
It should appear a grey line, like this
there are 2 possibility
Folder/file missing in your project directory
Missing "/" in the end of folder/file name in pubspec.yaml file
try copying the file manually not using VS-Code or Android studio. don't use drag/drop.
You need mention as
assets:
- assets/icons/
in pubspec.yaml
I had this same problem, I was able to fix it by using only the name of directory created in the projects root folder, instead of also including the file i was trying to point to.
assets:
- images/
is what worked for me instantly, just point to the whole dir and specify the file name in ImageAssets object.
Sometimes you need to enter filename as such:
flutter: assets:
- lib/assets/logo.png
I was facing the same problem, and I had two mistakes:
When I add lib in assets then I solve my problem.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- lib/images/background.jpg

ColdFusion Hardlink vs SoftLink traversal searching for Application.cfc (Solaris)

Using the following directory structure on a Solaris 11.3 box running ColdFusion 11. Apache is configured to follow Symbolic links. I have tried using all the following ColdFusion configuration options ( Default order, Until webroot, In webroot). The test2.cfm wont grab the #DATASOURCE# from the Application.cfm and so it is undefined.
/webroot
- Application.cfm
/dir1
/dir2
/dir3
-test1.cfm
/dir4 (softlink to random directory)
-test2.cfm
---------Application.cfm-------------
<CFSET DATASOURCE = "bob" >
<CFAPPLICATION NAME = "bob_app" SESSIONMANAGEMENT = "YES" >
<CFSET SESSION.USERID = "0" >
---------test1/test2.cfm-------------
<cfoutput>
The data source name is #DATASOURCE#
</cfoutput>
Any Suggestions on how to properly add the symbolic link so that search path includes the Application.cfm in the linked directory parent.
I suspect the traversal is getting messed up in the upward recursion because the directory operations are scanning upward from the absolute path of the symlinked directory - which is why symlinks aren't a good pattern for anything traversing up.
I would suggest not using symlinks for any directories which are extensions of the main application - other than static assets.
Alternately, though it's messy and less than ideal, you could just add an Application.cfm to those directories and add <cfinclude template="/Application.cfm"/> ( or include "/Application.cfm"; ) in a <cfscript> block to the beginning of the file.

how to include same file in different directory?

Directory structure :
Main directory structure
home.php
thankyou.php
Php directory structure
root directory/php/date.php
Include directory structure
root directory/include/header.php
root directory/include/footer.php
Images directory structure
root directory/images/logo.png
root directory/images/abc.jpg
For date.php, i want to use header and footer.php files. but images are not shown.
i written below code
<?php require_once '../include/header.php';?>
But when i used for home and thankyou.php, It is working fine.
In header file, code for images
<img src="images/logo.png" alt="Logo" class="image">
i dont want to change directory for date.php. And not interested to absolute path for images.
What to do so that images will be see in both directory()
i Used base html tag as doubleui told me in head section
<head>
<base href="root directory/images/" target="_blank">
</head>

Twig- Include Template from other Directory

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' %}

Resources