Go HTML templates for multiple sites - go

I'm preparing an application that will serve several different sites that have a common admin panel (a page with different themes to simplify).
Each of these "themes" will have different needs. For example while all of them show a list of services, some of them will show an associated image too. For the ones who don't I would prefer to avoid the DB calls to fetch them (different logic to render the pages).
In Laravel (a PHP framework) this would be a perfect use for view composers.
What would be the design of such a system in go?
I was thinking in some kind of "hooks" that each theme can register to run functions to fetch and add data for a specific template. There's a better way to do it?

If you pass a list of service objects to the template, you could easily decide inside the template what you want to show. If you decide to show a service's image, you could call the ServiceObject.ImageURL() (which would then lead to a database call). If you're not calling this function, there will be no database query issued.
A sample using the pongo2 template engine:
{% for service in services %}
{% if page.theme == "simple" %}
<p>We don't show any image in the simple theme.</p>
{% else %}
{# This would lead to a database call and return the image path #}
<img src="{{ service.ImageURL() }}" />
{% endif %}
{% endfor %}
Another idea would be to simply create an individual template per theme (extending the common base template); you don't have check for the theme inside the template then.

Related

How can I sort the featured blog posts in the top of the list in Octobercms

My request is simple. I want to keep my blog list ordered by created_at DESC but I want also to show the featured posts in top.
Let's say that I have 4 categories : Gold, Silver, Bronze and Other ...
I want to display the Gold posts first.
Then the Silver posts.
Followed by the Bronze posts.
And finally the others.
All of them should be ordered by created_at DESC.
Is there a feature out of the box to do that ?
Or should I create a new plugin to extend Blog with this feature ?
What do you think ?
I am assuming you are using the Rainlab Blog plugin. There are numerous ways to solve this and I don't think there is an official "way". Here are some examples that you will have to fit for your own code.
Solution with Twig. Twig has a sort filter which you can pass in an
arrow function, check
here. Then you
can do if statements to display gold to bronze.
{% for blog in blogs|sort((a, b) => a.created_at <=> b.created_at) %}
{% if blog.category == Gold %}{{ blog }}{% endif %}
{% endfor %}
Adhoc CMS
Page. Instead
of using the rain blag component you can use the plugin in the PHP
Code to the page/layout/partial. This is where give you the ability
to work with the model to organize it the way you want to. Here I am
using OctoberCMS querying
features.
use Rainlab\Blog\Models\Post;
public function onStart() {
$this['golds'] = Post::whereHas('categories', function ($query) {
$query->where('name', 'Gold');
})->get()->sortBy('create_at');
}
Third way and the way I recommend is to build your own plugin which
can either extend or filter the blogs posts like how I did in the
CMS Page example. Read the documentation here.
One side note that if you click into the {% component 'something' %} you can expand the htm template.

Add a section not working in Shopify Theme development

I have made a few websites before using a shopify theme and I am trying my best to develop my own theme. I understand how sections work, and I can add the sections that I have created in statically, but I cannot dynamically from the theme customizer. Has this happened to anyone before or know of a way to fix it? Instead of being able to choose a section after clicking add a section I just get the message
"This theme doesn't include sections that can be added to the homepage."
The first two screen shots are of my theme customizer, the third is of another theme and what it is supposed to look like. I was under the impression that all you had to do was use the conent for layout tag to add in your sections.
{{ content_for_layout }}
Figured it out! Every section has to have a preset in the {% schema %} otherwise it will not show up in the add a section area.
You put {{ content_for_index }} in the index.liquid file and your preset in the {% schema %} of yoursection.liquid
https://help.shopify.com/themes/development/theme-editor/sections
"presets": [
{
"name": "Collection Carousel",
"category": "Carousel",
"settings": {
}
}
]
Will make it appear like this:

Generate Markdown Posts in Jekyll and Made Available to Paginator Variable

Is it possible to generate a set of posts in _posts as Markdown files in a way that they are treated as if they had been manually created and therefore, available to the process of creating the site pagination? I found examples of how to generate pages in various places like this SO question, but they seem to put the rendered content in _site and displaying the {{ paginator.total_pages }} variable does not yield any value.
Yes its possible to add "dynamic" content using a generator plugin.
You can generate pages, posts, collection's items, static files, anything you want.
For the {{ paginator.total_pages }} returning nothing. The paginator variable will only be available in your pagination template and nowhere else.

Passing a YAML frontmatter variable from a page to a collection in Jekyll?

So i have a collection called components. The components are a series of small HTML objects with some data in.
I want to pull these components into a page, and be able to pass overriding YAML frontmatter from the page into the component to determine a couple of things on the component.
Is this possible?
I'm pulling specific components onto a page using the following syntax:
{{ site.components | where:"component_name","event-title" }}
but i'd like to be able to do something like:
{{ site.components var-page-state="offline" | where:"component_name","event-title" }}
I chose to use a collection than a series of includes, as i need to loop through the collection and generate/render an index page of all of the components on it automatically.
EDIT:
So i want my component to be able to output a variable set on the page it's included on.
component.title.html:
<h2 class="title">{{ page.var-page-status }}</h2>

Jekyll/Octopress: How can I use a tag inside a tag

In Jekyll/Octopress, how can I place the output of a tag inside another tag?
This is what I want to do (using Octopress's img tag):
{% img {% PluginThatOutputsAURL %} %}
If I do that I get this error:
Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | “title text” [“alt text”]] %} %}
Is it possible to do this? What would be the right syntax to do so?
Jekyll/Octopress uses Liquid Templates to parse these things.
As far as I know, you can't call two functions within a tag like that. You can however specify variable names in tags:
using Liquid variables inside of a liquid tag call
I tried Googling a bit and didn't see anything pop up for calling two functions within one tag.
IMO, I'd recommend creating a custom tag to do exactly what you want. I have created several custom plugins to Octopress because I needed such customization. The plugins, which is just Ruby. is pretty straight forward.
So, copy the existing img_tag.rb and call it, mycustom_img_tag.rb or whatever, and perform your magic within the render() method.
For example, here's my HTML5 audio player tag I wrote where I needed to know what extension the audio file was (specified in the url) in order to specify the content type:
https://github.com/eduncan911/eduncan911.github.io/blob/b89f47cd86c37f2cfb5c3093612fe0a049808325/plugins/audio_tag.rb
^- NOTE: It is incomplete and all the options doesn't work as I specified. You can see what works in the render() section, where I just parse the data-* attributes manually (I ran outta time and just made it work).
I basically used two other plugins as a template and created this one.
You have access to the entire Octopress methods and variables in your plugin - there are no restrictions to the entire code base.

Resources