new to drupal.
I have created a view called "my_view_2".
Inside my twig templates I use {{ my_view_2}} to render that view but nothing appears on the page.
If I declare the display of the view as page and assign a node to the view, then the view is rendered. But this way the node template is lost.
The above happens only to some views not very view I have and I can not find the differences between those that working and those that does not working.
any help is appreciated.
Looks like you want to create a view with some kind of display then use twig template to style it. If that's the case, then decide which twig template you want override and copy it over to your /theme folder.
Locate the template you wish to override.
Copy the template file from its base location into your theme folder.
(optionally) Rename the template according to the naming conventions in order to target a more specific subset of areas where the template is used.
Modify the template as you want.
With respect to views look at the below location for which template to copy/override :
core/themes/stable/templates/views/views-view.html.twig).
For example, if we want to override "views-view.html.twig" template for our view, the following template names are valid:
views-view--[viewid]--[view-display-id].html.twig
views-view--[viewid]--page.html.twig
views-view--block.html.twig
views-view--[viewid].html.twig
views-view.html.twig
Once you copy a template file into your theme and clear the cache, Drupal will start using your instance of the template file instead of the base version.
Reference:
Documentation:
https://www.drupal.org/docs/8/theming/twig/twig-template-naming-conventions
https://www.drupal.org/docs/8/theming/twig/working-with-twig-templates
Related
I'm new to Drupal and having a difficult time figuring out the file structure that I need to render my content correctly.
Here's what I have:
A content type called resource_library - with several resource items uploaded
A view called resource-library, which displays all resource library items.
A page template called page--resource_library.html.twig that currently has scaffolding (sidebar etc), then reffers to {{ page.content }} - which renders and loops the resource items from this file - node--resource_library.html.twig
Another file called node--resource_library--full.html.twig which displays full contents of the resource items.
The problem is that when I go to my /node/6 to view a single resource item I can see that it loads from page.html.twig, which does not have the same scaffolding similar to page--resource_library.html.twig.
My question is, is it possible to define the page template that will be used to display a single node? And if not, how do I achieve this? How do I make this specific content type node full views to look different/unique from any other nodes for content types?
Or am I going about this the wrong way? I'm working with Drupal9.
Has anyone else encountered this problem?
I cannot modify files of this type:
{% include 'mobshop/template/common/icons/wishlist.twig' %}
The file "wishlist.twig" is modified in the log but the changes do not appear live.
Do you have any suggestions?
Short answer: you can't modify with OCMod twig files that are added via include method inside the twig template.
How twig include works?
include is twig method that allows you to add partials into your theme which is a cool feature. when twig engin runs, it compiles the template by following the link and adding the html part into the final html output string.
How OCMod works?
OCmod is basically a function that takes in the path of the file (in our case the twig template file path) and after parsing the string modifies it and saves to the OCMod cache.
Then, when OpenCart asks for that file, the OCMod engine tries to first return the cached file, and if that is not available, then the original file.
so all files that are wrapped in modification('') method have this support.
The reason why twig include is not supported by OCMod
From the logic above we can see that the OCMod modification method simply never sees the twig partial file path from the Include method. It is jsut beyond its scope.
The modification method sees only a string {% include 'path-to-partial-file' %} and that is it. it never dives into that path and never tries to create a OCMod cache off of that file.
Conclusion.
You should not use "include" in your themes at all. Its just bad practice in OpenCart themes. Although personally I love this feature of Twig, I am also forced to avoid it.
The only way you should add partials in OpenCart is via the Controller ($this->load->controller('...')) attaching it to the $data field and then displaying it in the template.
And if you still MUST have this feature
PHP Twig engine is a powerful tool and you can extend it to your needs. You can still add an extension that can make the include method to work with OCmod, although I have never added that feature.
Here is a twig extension https://github.com/Dreamvention/2_d_twig_manager/blob/master/system/library/template/Twig/Extension/DTwigManager.php that you can use as an example and modify to your needs.
Enjoy!
I am using the latest NuGet version of orchard core. As I want to use Bulma as CSS Framework, I Need to create tepmlates. One of several Problems that I have is: How can I Access the field contents from within a liquid template.
For instance the Blog theme has the content type Article. Within Article there is a field Subtitle. I created the template Article-TextField as
<h2 class="subtitle">{{ Model.Field.Text }}</h2>
The similar .cshtml template works, but apparently with a liquid template, there is no way to Access the TextField Text property. How can I do it the right way?
The problem is that the OrchardCore.ContentFields module doesn't register the TextField type with the Liquid's TemplateContext.GlobalMemberAccessStrategy service, which prevents you from accessing any members on the TextField object.
The module does register DisplayTextFieldViewModel like so:
TemplateContext.GlobalMemberAccessStrategy.Register<DisplayTextFieldViewModel>();
Not only is this unnecessary because the LiquidViewTemplate already registers the model automatically, it also doesn't help you much, since you can only access the model's Field property, but not its Text property.
For example, this will yield some output:
{{ Model }}
This will too:
{{ Model.Field }}
But as you correctly noticed, this won't:
{{ Model.Field.Text }}
This is clearly an omission in Orchard Core, specifically in the ContentFields module.
Since you're using the NuGet packages, you can't hack it into the source code obviously. But what you could do is add a Startup class to your theme that derives from StartupBase and add the following static constructor:
static Startup()
{
TemplateContext.GlobalMemberAccessStrategy.Register<TextField>();
}
Make sure to reference the OrchardCore.ContentFields NuGet package and import the OrchardCore.ContentFields.Fields namespace for your theme to compile.
After that, you will be able to access the Text property from your Liquid template.
Finally, I would recommend that you file an issue on GitHub about this omission so that you can ultimately remove the code I mentioned from your theme's Startup file.
I am using one page template which contains master page layout also. Now when i access the page having layout which is displaying in browser. Now i wanted here if i use querystring like below:
http://localhost/Mywebsite/page?AppMode=true
then i wanted here to show only content of the page master layout should not be displayed with page content.
Currently i am using Kentico9.
A page's template is not something you can do with a macro, however you can control a Page Placeholder webpart with a macro/querystring.
So in order to do what you want, this is what you'll need to do.
For the page's actual template, it will pretty much just be a 'blank' page template, with a Page Placeholder webpart in it.
For the page placeholder webpart, set the path to "." (so it shows this page), and the Page Template you can then set with a macro: {% (Convert.ToBool(QueryString.AppMode, false) ? "NotAppModeTemplate" : "AppModeTemplate") %}
Tell me if that does the trick for ya! The only caviote is the only 'control' of the master template used is in the template settings, which can inherit the root or the nearest master template, so if you want one template to use 1 master page, and the other to use a different, you'll have to set the masterpages somewhere in their ancestry and then use the "Inherit from Level 0,1,2..." setting in the Page Template.
Assuming you're doing this using Kentico's ASPX mode of development. Do a simple check for the querystring and set the visibility of the placeholder in code behind. If I were doing this in Portal mode, I'd do that with a macro on a webpart.
I used in master page template conditional layout and that layout i used visible property with macro querystring with my condition except for my content place holder and it is working fine as i wanted.
According to the FreeMarker include statement docs, you can make header- and footer- aware templates like so:
<#include "/header.ftl">
<!-- Content of my this page -->
<#include "/footer.ftl">
But if my web app has hundreds of pages/views, this is a lot of redundant copy pasta. It would be great if there was like a "layout" concept in FreeMarker, where I could say "Hey, here is a layout:"
<#include "/header.ftl">
<#import_FTL_Somehow>
<#include "/footer.ftl">
And then create separate templates for each view/page (index.ftl, contactUs.ftl, etc.) and then tell FreeMarkers which FTL files "use" the layout. That way I wouldn't have to keep specifying header/footer includes in each and every template file.
Does FreeMarker support this kind of concept?
It doesn't, though if you only need a footer or header it can be solved with some TemplateLoader hack (where the TemplateLoader inserts the header and footer snippets, as if was there in the template file). But the usual solution in FreeMarker is calling the layout code explicitly from each templates, but not with two #include-s directly, but like:
<#my.page>
<!-- Content of my this page -->
</#my.page>
where my is an auto-import (see Configuration.addAutoImport).
Update: Another approach is that you have a layout.ftl like:
Heading stuff here...
<#include bodyTemplate>
Footer stuff here...
And then from Java you always call layout.ftl, but also pass in the body template name using the bodyTemplate variable:
dataModel.put("bodyTemplate", "/foo.ftl");
cfg.getTemplate("layout.ftl").process(dataModel, out);