A beginners tutorial on how grav templates work

In answer to @phil’s question in another thread, here is an explanation on what are grav templates.
I created this thread so the explanation is not lost in that other thread, in case some day it’s useful to someone else browsing the forum. Hope it can be of some use for beginners.

Intro

To customize what grav or it’s theme do more than the settings proposed for tweaking by themes and plugin developpers, one should have a basic understanding of what are HTML, CSS and eventually JS.
There are thousands of websites, tutorials, videos… explaining how to create basic pages with HTML+CSS+JS, and I think that’s a necessary starting point to be able to understand grav templates.

A little reminder of what HTML and CSS do

HTML looks like this:

<div id="some-id">
	<span class="some-class">
		<h1>Phil</h1>
		<img src="https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.getgrav.org/phil/96/7554_2.png">
	</span>
</div>

This means I have a title (h1) and and image (img) elements which are in a span container element, which is itself in a div element.
Notice all the <something> and </something> start and end tags?

This is how it looks:

Now if I want to style this, I’ll use CSS:

#some-id {
	background-color: red;
}
.some-class {
	color: blue;
}
.some-class > h1 {
	text-align: center;
}
img {
	width: 500px;
}

This will color the background of my div container red.
The span will be in that container, so I’ll have blue text on the red background.
The title (h1) will inherit the color of it’s parent, so will be blue, and will be centered.
The image (img) will have 500 pixels width.

In this case only the h1 title inside elements with the some-class class will be centered.
So if I have other h1 titles I would like to center, I can change this par of my CSS from:

.some-class > h1 {
	text-align: center;
}

to

h1 {
	text-align: center;
}

Now the templates

Ok so what are templates?
Well templates are a way to extend HTML. It’s an additional syntax that lets grav build HTML from different pieces of code. So you don’t have to write HTML yourself.
Grav templates use the twig syntax which is recognizable from it’s use of curly brackets: {% something %} and {{ something }}.

Now to use our previous example in some pages, we could create the following templates in our theme:

templates/example.html.twig:

<html>

<head>
	{% block stylesheets %}
		{% do assets.addCss('theme://css/example.css) %}
	{% endblock %}
</head>

<body>
	{% include 'partials/image_on_red_background.html.twig' with { image_title: page.header.title, image_url: page.header.image } %}
	{% block content %}{% endblock %}
</body>

</html>

templates/partials/image_on_red_background.html.twig:

<div id="some-id">
	<span class="some-class">
		<h1>{{ image_title }}</h1>
		<img src="{{ image_url }}">
	</span>
</div>

css/example.css:

#some-id {
	background-color: red;
}
.some-class {
	color: blue;
}
.some-class > h1 {
	text-align: center;
}
img {
	width: 500px;
}

Now we can create a new page, choose the example template in the grav admin (or markdown header) with the following content (to customize the frontmatter in text mode (what’s here between the --- ---) in grav admin you need to choose “expert” mode):

---
template: example
title: Phil
image: https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.getgrav.org/phil/96/7554_2.png
---
This text will be displayed under the title and image.

Your page, when saved will look like this:

This is an extremely simplified example of how templates work.
But in short, one can create as many templates as they want, they can include them in one another. Templates make it easy to build blocks that can be used and reused in different context to create pages from these blocks. For example a menu can be a block included in many different pages, or one can make a feed block…
Tweaking templates to make them do what one wants is fairly easy when one understands the basics of HTML and CSS, the grav and twig docs explain pretty well the specifics of the template syntax.
Once you get into it, the risk is that starting from little tweaks on an existing theme you end up liking it and creating you own theme :wink:

3 Likes

I hope this was useful for some people. It looks like a solid explanation though I admit I have not read all of it.

One issue for me is that you seem to refer to Grav templates a lot. They are most definitely Twig templates, although it’s fair to say that Grav provides well-documented extensions to vanilla Twig, so they do have their own flavour. Just wanted to add that, sorry if I overlooked something.

2 Likes

@squeak Thank you very much for creating this tutorial. That was very kind and thoughtful of you. It helps me get a better understanding. By the way, I did go through an online tutorial on HTML and CSS which was extremely well done so I do have a reasonable understanding of those topics. I have not gone through any tutorials on javascript so have little understanding of it.

The conclusion I came to after going through the HTML and CSS tutorial is that getting into all the details of developing websites is something that is not practical for me to do especially since I am not young. That’s why I turned to a way of developing websites that is simple and easy to understand like Google Sites. I started to use free software that was fairly user friendly by a company I no longer remember the name of, but that company was purchased by Google. Google now has classic Google sites and new Google sites. The classic sites are those created using the original software from the company Google purchased.

Here is how I currently think of the situation regarding developing websites. There are those, like Google, that are developing software by Geeks but for everyone. Then there is software being developed by Geeks for Geeks. I put Grav in that second category. There is no way the average individual would be able to use Grav to develop a website, but they could with Google Sites.

Unfortunately, there are limitations to Google Sites. One of the limitations is that you cannot create a private Google site unless all the users have a Google account. That’s the limitation I was trying to get around by using Grav or some other software like mediawiki or Docuwiki or Grav. Now, sadly, it does not appear that it is possible to create a private site like other private sites using Grav either.

Hey @phil,

I’ve never used Google Sites, and avoid non-free software as much as possible, so I don’t know how it works in details. But like others recommended already, if you want a user-friendly, well developed software/service that let’s you do pretty much everything from an interface and that’s usable by non-geeks, I’d suggest wordpress. It’s free software and is a powerful tool to create websites, and I’m sure you’ll be able to achieve what you want through it.
Wordpress is one of the most used website builder in the world, so that gives you an idea of it’s possibilities.

I personally use grav because I find it nicer than wordpress, but that’s because I usually prefer looking at a text file or terminal than an interface in the browser. I’m convinced that grav can achieve what you want to achieve, but don’t know exactly with how much tweaking of theme templates, or plugins.
Also grav has many qualities, it’s light, fast, powerful, very well designed, and really simple to use when you understand it’s logic (which is also fairly straightforward). In short I think grav is one of the best site builder one can find, but that’s a question of taste :slight_smile:

1 Like

It is clear that end-users at one end and Grav and this community at the other end are not a match made in heaven.

Grav

Grav is a CMS created by developers with developers in mind. The documentation is also written by developers for developers. The documentation is about the bits and bytes of Grav itself and the reader is expected to fill in the left out parts that are considered to be known.

The community

The community is a group of volunteers consisting of Grav enthusiasts and developers, who like to help when someone gets stuck. The help often consists of hints to resources, or small code snippets to get a fellow member back on track. This is in most cases enough for the experienced user. It is also a good balance between effectiveness and the precious time spent by the volunteers.

End-user

An end-user might be able to use Grav if the functionality of a theme and plugins provide out-of-the-box what the end-user needs. However, as soon as functionality is required beyond what the theme/plugin provides, the end-user will be confronted with technical documentation, yaml, css/scss, twig and sometimes PHP.

What an end-user cannot expect the community to do, is writing example websites and step-by-step elaborating tutorials going from high-level schemas down to code to fill in the needs and knowledge gaps of the end-user. Because 1) these kind of tutorials are very time consuming to create and maintain, 2) each end-user requires different use-cases to be documented and 3) the end-user is not the target group of Grav.

In conclusio

If there is no match made in heaven, both ends need to consider if one wants to continue.

2 Likes

As you all know, many websites today have a public page and a private part such as websites for banks, medical clinics, etc. where the user, once they create their account and enter some id and a password, can access the public page, identify himself/herself, enter a password and then, if everything is OK, they can enter the private part of the website.

If I were a professional web developer, would I be able to develop a website with the above properties using Grav with the login plugin as it exists today?

@phil, A few things..

  1. As asked a few times before… Don’t burry unrelated questions inside an existing topic!
  2. You’ve asked this before and received an answer, but you didn’t bother to respond to requests to clarify where you got stuck: Making Grav website private
  3. As follow up on my reply above: I’ve considered our match… I hate ghosting, so this is to let you know this will be my last contribution. I’ve contributed my fair share…

@pamtbaau, I didn’t even know what ghosting was! I had to google it! :slight_smile:

@Pamtbaau, One other thing, when you’ve asked me in the past what I did precisely and what result I got, and I didn’t reply, it was because I didn’t remember what I did. I hate to admit it since I always think of myself as young, but the truth is, I’m an old man and my memory is very poor plus I’ve done a thousand things - a lot of them by trial and error and just guessing as you can see from the Google site I posted - and I don’t remember a lot of it. If I don’t write everything down, I forget almost everything - at least the details.

You might want to look into Joomla. I use Grav for my personal site, but I use Joomla for a different site that needs exactly what you describe: a public section and a section for members only. Wordpress can do this with plugins, but the feature is built into Joomla and it works for my purpose. Joomla has a bit of a learning curve, but I haven’t had to use any custom HTML/CSS/Javascript yet to get the results I need.

@dmeissner, Thanks for the tip, but I’ve already done so much work on the site I’m developing using Grav, I’d like to be able to finish it with Grav if possible. I’m not young, and it takes me a very long time to learn new things. I’ll keep your suggestion in mind. There seems to be a whole lot of alternatives to choose from.

Hey @phil,
I just wanted to comment on you saying that google sites is free. I talked about grav being free software, not free as in “you don’t need to pay money for it”.
It might seem you don’t need to pay for google sites, but actually you do pay for it, just not with money. Just like a cow doesn’t pay to live in a farm, it’s free for them to use the farm, is it?
Wix is like google sites. Except maybe it has a different business model based on making your pay for certain features.
On the other hand, Grav, Mediawiki, Dokuwiki and Wordpress are “free software”, which means you can know what they do and install them wherever you want (grav may have some non-free extensions and themes, but it’s core at least is free-software).
If you choose to use wordpress.com, or any other platform proposing a free software preinstalled, they might make you pay for it, I guess grav does the same somehow. But in that case you’re not paying for the software, but for renting the hardware running it and for the maintenance of the service. Which is work, so it makes sense to pay for.
If you don’t want to pay for that, you can install them on your own device, which seem to be what you’re doing having your site hosted by ionos. And I guess you’re paying for that.

That said, to answer your last question:

I think the answer is clearly “yes”.

Hello, @phil I will write about my experience in dealing with documentation to aid memory. I have a huge file with an outline in a tree format, each node (a note) of the tree might have multiple sub-nodes, and each node has keywords, and the application helps me to find a particular word in certain branches of the tree or in the whole tree.

Each note can be very large, with text, images, screen captures, code boxes, tables, and even parts copy-pasted from books or websites, with a complete procedure or the path that let me discover how to do something, documented step by step, until I find a solution, so next time I only need to vaguely remember one or two words, search and find what I have done, specially in complex tasks. In simple words: a personal knowledge base. The price to pay is the time documenting; the tool is very good and free, here is the link:

I hope this helps you.
Regards.

joejac,

Thanks. I only wanted to use Grav for one website and with the help of the AI Claude I was able to finish it. Your documentation sounds like a good idea. However, for me, using Claude worked out well after I figured out how to effectively interact with him/her/it. I only wanted to use Grav for one website, and I have it finished now.

Regards,

Phil White

1 Like

You might use something like BludIt. It is very simple to use. The formatting is very basic, but the basic functionality is well explained and quickly understood. You can drop in images and links like in email. I believe you can create users and give specific people permission to visit the site.

To use it you will need hosting and a domain name registered (yoursitenamedotcom). If you find a host with the softaculous installer (common among hosts for installing open source software) that will make your life easier. You just choose the software and it installs it for you. I believe bluehost.com in the US has softaculous and orangewebsite.com in iceland does. Bluehost has decent support for non-technical users and orangewebsite has excellent support for us. Both are modest in cost. Softaculous also includes grav, if you ever are ready to graduate.

Best of luck, Phil.

Wow