Custom page variable in OctoberCMS - php

Is it possible to use custom page variable in Configuration section in OctoberCMS?
When I do this:
url = "/blog"
layout = "default"
custom_var = "value"
==
{{ custom_var }}
my custom_var is deleting when edit page from admin panel.

It seems this was the old way of defining all stuff in single view (visual representation)
now its divided in 3 portion
1st name and url(slug) then 2nd markup(html) section
3rd code section
so you can follow new way of declaring things and it should work
use code section add this
public function onStart() {
$this['custom_var'] = 'some value';
}
use markup section and add this
<h1>{{ custom_var }}</h1>
it will work, still any issue please comment.

Yes, its possible to set custom variable into page. you can try the following code into the php section
function onEnd() {
$this->page->custom_var = 'some data';
}
And then you can use this custom_var into your layout file like
{{ this.page.custom_var }}

If you want to define new variables inside markup you can use following syntax:
{% set custom_var = 'some data' %}

Related

How do I use variables in a different place?

I'm trying to use a variable in a different place but it doesn't seem to be working.
Basically the idea that we had is to use the data you assign in the admin panel for your contact info that will be displayed on the contact page onto our footer. But whenever we try to use {{ location.adress }} in our footer.twig file it returns nothing.
To insert anything in view in .twig file you have to determine it in corresponding controller file, like #K. B. said.
In this particular case open /catalog/controller/common/footer.php
Find
$data['newsletter'] = $this->url->link('account/newsletter', '', true);
Add after
$data['address'] = nl2br($this->config->get('config_address'));
Now open /catalog/view/theme/YOUR_THEME/template/common/footer.twig
And place anywhere you need
{{ address }}
Than clear TWIG cache. Done. Now the address from the settings is in your footer.
If you wish to retrieve some data in your template, you must declare that data in corresponding controller file. For example if need retrieve {{ address }} it should be declared $data['address'] = 'data_retrieved_from_db'; For admin and for catalog are different files.

Dynamically grab latest post OctoberCMS Blog plugin

I asked this question in the October forums but 12 hours later the post still says "unapproved" and I don't believe it's visible. Here's a copy-paste:
What I want to do is grab the latest blog post and display it on the homepage of the website. I can do it currently with my own post partial and the posts variable injected by the blogPosts component like so:
[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 10
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "blog/category"
postPage = "blog/post"
==
{% partial 'site/blogpost' post=posts|last %}
However, I'd like to do this with the default blogpost component that comes with the plugin, but the only way to pass the post to the component seems to be by using the slug in the url, which doesn't really work for the homepage. How can I achieve this?
It is possible to use blogPost component but fetching last post slug and pass to it seems not good practice
what you do is you can use [blogPosts] component and set proper setting there to get latest/last blog
to make it possible
Posts per page : 1 [ as we need only last latest post ]
Post Order : Published(desc) [ you can change as you need ]
now just use proper markup to render it use default or just override to customise it.
Note : It will return list of post but in list there will be only 1 post as demanded so for custom markup just take care of that.
if any doubt please comment.
So, as with most things when it comes to development... RTFM.
All the info I needed was in the Components section of the October CMS docs, and of course I was only looking in the plugin docs. What I ended up doing was overriding the default component partial with my own and then passing the slug to the component. My original reason for wanting to use the default partial was that my custom partial wasn't rendering images, but the default would. I just needed to steal the line <p>{{ post.content_html|raw }}</p> to get that to work.
[blogPost]
==
...
{% component 'blogPost' slug=posts|last.slug %}
Additional info: With the above solution your template pulls all blog posts in the database, which means if you have a lot of posts this could (and most likely will) affect performance. Turns out October templates have a PHP section. The proper thing to do is to remove the blogPosts component and grab the latest post Model like so:
[blogPost]
==
<?
use RainLab\Blog\Models\Post;
function onStart()
{
$this['latestPost'] = Post::latest()->first();
}
?>
==
{% component 'blogPost' slug=latestPost.slug %}
Note: The blogPost component injects a post variable which will override a post variable defined in the PHP section. This is way the variable is labeled latestPost.

Passing a field name from CFS wordpress plugin through to twig

so I'm very new to PHP and I'm having a little trouble passing a field name through.
I am using custom fields (cfs) and writing in twig for php.
Currently, I just have in my page.twig. The idea being, if an image has been set in Wordpress, the image will display in location set in page.twig.
The field name is header_image
{%if header_image %}
{{header_image}}
{% endif %}
When using Timber you can easily pass variables from your PHP controller through to your twig view. There are 2 ways to do this:
1) In functions.php you can pass variables globally to the context. The Timber Starter theme has this in it for reference. Your add_to_content function, should look like:
function add_to_context( $context ) {
global $cfs;
$context['header_image'] = $cfs->get('header_image');
Think of $context as a giant variable that passes through your variables into that actual view.
Remember: this makes your variables globally accessible, so you will need to name your CFS id's uniquely. For example global_phone_number.
2) To better structure your code, you should pass through your variables to the context, via the controller for that page. This helps ensure that you don't pass around variables that aren't being used.
In your page.twig file, you will need to add:
$context['header_image'] = $cfs->get('header_image');
Remember: the $context name is what you use in your twig file, and $cfs name is what you called it in Custom Field Suite.
Lastly:
The {% if header_image %} is good, as this will check whether a variable has been set, and then decided whether to show this block.
However, in CFS, when an image is uploaded, it gives you the option to select File URL or Attachment ID. This means you will need to handle this result in your twig file.
For example, by choosing File URL, you will need to output the {{header_image}} as:
<img src="{{header_image}}" />
You may also then want to add in the alt attribute based on the images alt text set in Wordpress media library.

How to add a secondary menu to Bolt CMS?

The default way to add a menu to a template::
{{ menu(identifier = 'footer',
template = 'partials/_sub_menu.twig',
params = {'withsubmenus': false, 'class': 'inline-list align-right'}**strong text**
) }}
But how to build a secondary menu from other files than menu.yml?
To add a secondary menu go to menu.yml add for the mentioned example this lines of code:
footer:
- label: Imprint
title: Go to Imprint
path: page/imprint
class: first
The documentation can be found here: https://docs.bolt.cm/3.0/content/menus
Further explanation:
All menus used have to be declared in menu.yml.
To render them correctly you can call them in two different ways.
Example #1:
{{ menu(identifier = 'footer', template = 'partials/_sub_menu.twig') }}
Example #2 (Short hand syntax):
{{ menu(footer', 'partials/_sub_menu.twig') }}
Note: You can define more than one menu in your menu.yml file, but you
should define only one menu in each template file. So, if you have
multiple menus that should be rendered with different HTML, you should
have as many _menu_menuname.twig files in your theme.

Is it possible to implement an index.php file that can show one or other Twig template?

Let's say we have to manage user login and logout.
We have an index.php file that by default shows the index.twig template (which contains a header to allow users login or signup.
And we have another Twig template (welcome.twig) that's similar to index.twig template but, its header shows access to user profile, logout option and operations that our user can do on the website.
I'd like to know if the index.php file can show one of both of those templates by a conditional.
In my index.php file I've got this:
if (!isset($_SESSION['account'])){
$twig->display("index.twig");
}else{
$twig->display("welcome.twig");
}
As you may know, I am telling to show index.twig (the default template) when account is NOT set in $_SESSION variable, and to show welcome.twig when account is set in $_SESSION variable.
Account set on $_SESSION variable occurs in other file called login.php
For now, I've been using a second file (welcome.php) to get what I want, but I'm not sure about this is a good way to get it...
Thanks.
You should use conditional inheritance on your template. Look at this answer:
Twig extend template on condition
You can pass a variable to the template, like this:
$twig->display("index.twig", array('logged' => isset($_SESSION['account'])));
And then, do the condition in template with that variable. It can inherit from two templates, each with a different menu, depending if the user is logged or not.
I hope it helps.
I came across with this one solution:
I setted a value to the $_SESSION variable in my index.php file
<?php
require_once '../vendor/autoload.php';
require_once '../generated-conf/config.php';
require_once '../vendor/twig/twig/lib/Twig/Autoloader.php';
session_start(); // Session always starts when index.php is loaded (even if it is loaded for the first time)
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates/');
$twig = new Twig_Environment($loader);
// Condition to show any or other template
if (isset($_SESSION['online']) && ($_SESSION['online'] == true)){
$args= array('online' => true, 'session' => $_SESSION);
}else{
$args= array('online' => false);
}
// Display Twig template
$twig->display("index.twig", $args);
?>
And in my Twig template (index.twig):
{% if online == true %} {# It's not the $_SESSION variable 'online' value, but the 'online' value of the 'args' array #}
{% include 'userMenu.twig' %}
{% else %}
{% include 'defaultMenu.twig' %}
{% endif %}
That's making it work :)
I hope it helps someone.

Categories