Hot answers tagged

14

Twig variables are only accessible inside their own scope. . What is a scope: Tags like {% block %}, {% for %}, {% nav %} create a 'scope', which means that variables defined inside them, can't be accessed from outside. This is similar to local variables inside php-functions: {% block A %} {% set var = 'apple' %} {{ var }} {# This prints apple #} ...


11

You should never edit the core files (i.e. anything below /app), because whatever changes you add might be overwritten any time Craft is updated – or even worse, your amendments might conflict with new features added in Craft updates to a different part of the codebase; crashing your entire site. Seriously, don't do it. Whenever you need Craft to do ...


6

I believe what you asking about are Hooks and Events. Craft has several of them listed in the Hooks Reference and Events Reference. And Craft also allows us to add Hooks and Events in our plugins, which other plugins can leverage. In the case of Sprout Forms, as of this post, we don't have any documented events yet, but there are a couple in there and we'...


4

It seems to be what you need is Twigs extend functionality. What you have here is a very common template structure so there is nothing out of the ordinary! Firstly, going from your code I'd recommend your main layout template file looks something like this. The idea here being that these elements will not change for each page, but the content or information ...


3

You could define a variable in you child template: {% set bodyAttributes = 'onload="load()" onunload="GUnload()"' %} And then output the value in your parent _layout.html template. Use the is defined test, so that not every template extending this file has to have that variable defined. I'm using ternary syntax here, to keep the code nice and lean: <...


3

So I tried it, made three files, one extending the first, and another extending the second, and it all worked more or less as one would expect. bozo.html <!DOCTYPE HTML> <html> <head> {% set title = title ~ 'Main' %} <title>{{ title }}</title> </head> <body> Where was {% block whereWasBozo %}Bozo?{...


3

I found the solution of this was under my nose, I added this public function populateElementModel($row){ $model = Plugin_ClientModel::populateModel($row); return $model; } In Plugin_ClientElementType and then getCpEditUrl() it works.


3

There are two ways you could get something like this to work: If the other plugin fires an event when at a point in the code where your plugin wants to do something, you can just listen for that. (You will have to look through the plugin’s source code to be sure, if they haven’t documented it.) This is the cleanest route, so even if the event doesn’t exist, ...


3

This article may help as well: Twig Processing Order & Scope


3

My setup regarding includes for templates is as follows: I'll have a 'site' structure that looks at /pages/_entry.twig for it's template. But I'd have a range of entry types within this structure, for example: Homepage Contact News Standard content Then my /pages/_entry.twig template looks like this: {% extends "_layout/base.twig" %} {% block content %} ...


3

Unfortunately, there's no way around having the {% block %} tag somewhere in the template that's being extended. In other words, your main layout template it's going to have {% block %} tags. However, you can use the {% embed %} tag to "wrap" a block in a third template (e.g. "components/main-wrapper.html"): {# base.html #} <body> {% block main %}...


2

By setting the regenerateSitemapsAutomatically to false, SEOMatic won't regenerate a sitemap for every entry saved. At the end of the job, I set it back to true and save the first element inside of the same section. Changes to the original code: Seomatic::$settings->regenerateSitemapsAutomatically=false; // Disable it while (($csv = fgetcsv($handle)) !...


2

In your example, if you do this: $var = new \XXX(); $var->doThis = 'CONTENT HERE'; Then doThis will never exist for class \XXX because \XXX isn't where the method was defined. It would work from your extended class, though: $var = new YYY(); $var->doThis = 'CONTENT HERE'; Keep in mind that (usually) if the vendor plugin instantiates any instances ...


2

I use extend for different entry types. So i have a master.html with the main html construct and includes page parts like header.html and footer.html Then i have a standard.html that extends master.html and include the content templates. Entry Types with different page structure like projects.html or gallery.html also extend master.html, but can have ...


2

Hmmm I think I've got it already: _layout.html <!DOCTYPE html> <html lang="de"> {% include "pages/partials/_head.html" %} <body class="body"> {% include "pages/partials/_header.html" %} <main class="main-wrapper"> {% block content %} MAIN CONTENT PLACEHOLDER {% endblock %} </main> {% ...


1

I've built a plugin which uses it's own events before, I guess the way to do it would be the same for separate plugins, however this is untested for your use case. This should go in your plugin service // Import the event from the other plugin Craft::import('plugins.otherPluginHandle.events.OtherPluginEvent'); /Initialise the plugins event and pass ...


1

You could use include...with, as specified in the Twig Docs here Example for your case: {# in Template C #} {% include 'template-d.twig' with {'foo':'Hello World'} %} This should give Template D access to foo. Hope that helps!


1

Custom Asset sources are not possible in Craft 2 without some core hacking. However totally doable in Craft 3 (even in the dev preview). There is an example of how to pull it off here.


1

When extending Craft's active record classes (like UserRecord), you'll only be able to setup the AR relationships one way (from your class to Craft's i.e. MyPortal_ProxyUserRecord => UserRecord) as you've done. Setting up the relationship in the other direction (UserRecord => MyPortal_ProxyUserRecord) would require hacking core Craft files (not ...


Only top voted, non community-wiki answers of a minimum length are eligible