Drupal 8 Attached library to a paragraph - drupal-8

How can I attached a library to an specific paragraph? This is what I have so far.
function THEME_preprocess_paragraph(&$variables){
$paragraph = $variables['paragraph'];
$parentBundle = $paragraph->getParentEntity()->bundle();
}

If you still looking for an answer, you can easily do this via twig. Just create a twig template per paragraph, and add this: {{ attach_library('theme/library') }}.
You could do this via your .theme file as well, but that might be overkill for what you need.

Related

How can I configure VS Code to work with Prettier HTML formatter?

I am trying to have VS Code format my Django HTML files but I am getting:
There is no document formatter for 'django-html'-files installed.
The solution I found on the web works with Beautify, not Prettier.
How can I make it work with Prettier?
#Tedkovsky's answer might technically address the error you're getting, but once you're past that, you'll see that Prettier will mangle your templates since it tries to break long lines containing template tags like {{ }} and {% %}.
This is because Prettier currently (as of 2021-01-09) doesn't support Jinja or Django templates, and for the time being, it looks like the developers aren't interested in adding this functionality. There are 2 (closed) tickets about it here:
https://github.com/prettier/prettier/issues/5581
https://github.com/prettier/prettier/issues/5754
I wasn't able to find a plugin for it either, so it doesn't look like there's a solution for using Prettier with Django templates.
edit: I've been following this thread in the Django forum about autoformatters for Django templates. Perhaps something might materialize there.
later edit: Looks like djhtml can handle indentation, although it's separate from Prettier. It doesn't do full autoformatting, though.
even later edit: djlint can also be used for formatting templates
In settings.json, try
"[django-html]": {
"editor.defaultFormatter": "prettier"
},

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}

modx is only showing html code, not the template

I have installed ModX and Downloaded the "Basic" Template in the Extensionmanager! I chose the Basic Template at my first Page for the Resource! I cant find a Globalsetting to choose an Template? My problem is that if iam going to the site i only get the pure html code shown, no website! Why is this?
AFAIK there is no "Globalsetting" to choose a template. One of the nice things with MODX is that each resource can specify which template to use. So you can mix and match as required.
You could check if you have set the base href tag in head like this:
<base href="http://www.yoursite.com"/>
It will tell the site where to start looking for the other files that you have included in your markup.

Django: DRY Internal links in TextFields

I have a site with a bunch of 'Projects' which often refer to each other in their descriptions (stored in a TextField).
Rather than hard-coding the links between projects in their descriptions, I'd like to keep things DRY by referring to them using some sort of token, for example, in the description field:
Blabla text describing this project, this project was inspired by
{{ project "ProjectB"}} and lead to the development of {{ project "ProjectC" }}.
Which is then processed and turned in to:
Blabla text describing this project, this project was inspired by
ProjectB and lead to the development
of ProjectC.
To be clear: the description is free text which can contain none to many references to other items as hyperlinks at various points in the text. In a CMS this effect is usually achieved through some way to link to items by node/object ID - so that if the link changes, the reference can still be followed.
I've considered:
Evaluating the text field as a Template and using the 'url' templatetag in descriptions. Seems like the easiest solution but that templatetag isn't particularly friendly for content editors and evaluating each description through the entire Template renderer seems a bit cumbersome.
Implementing a templatetag which just re-implements a basic faux-templating system to just parse out a nice simple tag just for this purpose.
Extending the TextField to pre-process the description before it's saved to the database.
Has anyone done anything similar? What would you suggest?
I just answered a similar question on SO, and it seems like it might solve your problem as well (if by chance you're still looking for an answer three years later).
I wrote a template filter to parse the custom internal link format in the Textfield before display. I'm using Markdown to parse my textfields, so I made the links return in Markdown format, but they could easily be written as HTML instead.
Check out my answer here.
Update: I posted a revised version on djangosnippets.org that resolves internal links inside a markdown-formatted link, as well as on their own.
if I got your problem, you should use a custom template processor to pass a dictionary to your templates:
in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"myapp.myprocessor.foo",
)
in myapp/myprocessor.py:
from django import template
def foo(request):
ProjectA = get_Project_from_database
t = template.Template(ProjectA.html)
c = template.Context({'name': ProjectA.name})
rendered_ProjectA = t.render(c)
return { 'rendered_ProjectA': rendered_ProjectA }
or if you don't wanna use Django template system you can use regular expressions (import re)

How to include javascript script links to pyrocms page?

I'm using pyrocms to develop a system.
I know that, to include style links in header tag ''
in a page is by using $this->template->set_metadata().
But how can I include javascript links like that?
Any answer is appreciated.
Alternatively, if this is for a theme and the script is housed within your actual theme/js folder, it becomes:
{{ theme:js file="file.js" }}
Using just the {js} function would send it to the actual system's embedded js files.
$this->template->append_metadata(js('foo.js)) will work, or you can dump it into the view as others have suggested.
If you are creating a template you can do it like this:
{js('file.js', 'modulename')}
See the Pyro documentation.
If this is not the answer you are looking for, please explain more clearly what you want. E.g. in which file exactly do you want to include your javascript?