After programming plain HTML/CSS/Javascript/PHP I've just started using the CMS called OctoberCMS since both the Laravel framework and OctoberCMS look very well structured and easy to use/maintain. But I'm a little confused on how to process a single detail page or a overview page.
Let's take a news page for example. So far I've made this page:
title = "News"
url = "/news/:news_id?|^[0-9]+$"
layout = "default"
description = "This is the news page."
is_hidden = "0"
meta_title = "News"
meta_description = "News page meta description"
==
<?php
function onStart()
{
$news_id = $this->param('news_id');
if(isset($news_id)) {
$news_article = []; //get the news article by id
$this['news_article'] = $news_article;
} else {
$news = []; //get an array of news articles (last ... articles ordered by datetime desc)
$this['news'] = $news;
}
}
?>
==
<div class="container">
<h1 class="block-title">
News
</h1>
{% if news_article is defined %}
Article
{% else %}
Overview
{% endif %}
</div>
But where do I actually manage to make some sort of library for my news articles? I've read something about creating a new class in a new plug-in but I can't find any tutorials or documentation for this problem, or I'm just using the wrong terms while searching. Can someone make a small example (maybe with news articles) or post a link where I can find a tutorial/documentation?
That is more comfortable to use plugin instead for write all code yourself.
Rain lab plugin allow create, manage, categorize, edit all kinds articles (include news).
You can get admin part from that plugin and use your visitor view.
Documentation: https://octobercms.com/docs/plugin/registration
If you want to generate some code in command line here are some useful commands:
Generate plugin registration file and folders
php artisan create:plugin AuthorName.PluginName
Generate model
php artisan create:model AuthorName.PluginName ModelName
Generate controller
php artisan create:controller AuthorName.PluginName ModelNames
Refresh (reinstall) plugin
php artisan plugin:refresh AuthorName.PluginName
This should get you going, docs will be helpful after that.
Use Builder (https://octobercms.com/plugin/rainlab-builder) plugin to manage CRUD very easily.
Suppose you have a model named NewsModel and you want to show the news listing or a single news in the frontend then you can modify your code by the following..
N.B: No need to write php opening and closing tag in the php section, just write
use Namespace\Plugin\Models\NewsModel; //needed to get data through model
function onStart()
{
$news_id = $this->param('news_id');
if($news_id) {
$news_article = []; //get the news article by id
$this['news_article'] = $news_article;
} else {
$news = []; //get an array of news articles (last ... articles ordered by datetime desc)
$this['news_list'] = $news;
}
}
==
<div class="container">
{% if news_article %}
<h1 class="block-title"> News Details</h1>
<div>{{ news_article.details }}</div> <!--Suppose you have a field named 'details' in the news table -->
{% elseif news_list %}
<h1 class="block-title"> News List</h1>
<ul>
{% for news in news_list %}
<li> {{ news.title }}</li><!--Suppose you have a field named 'title' in the news table -->
{% endfor %}
</ul>
{% else %}
No news found !
{% endif %}
</div>
Related
does anyone know now to create a custom view type for ez platform? The default 3 have been exhausted and we need a new one for 'link'
Alternatively, does anyone know how to use the render( controller( with a custom template as this would also resolve out block right now.
Basically, we have a multi-relational field in a content object used and we need to print links to all the related contentIds, path works great but we cannot find a way to extract the name of the content object for the link without doing some fairly funky tpl logic of passing in params.
EG: As a hack for now we can pass in "embed_type" as a custom param with the render(controller("ez_content:viewAction" to pull in an alternate view for the content object for a specific content type and view type.
{% if embed_type is defined %}
{% include "embed/#{embed_type}.html.twig" %}
{% else %}
<h1>{{ ez_field_value( content, 'name') }}</h1>
{% endif %}
However, this is very ugly and all we really want to do is use 1 template for all content types, so all we need to do is loop through the relational field and print links (as the only thing available in the content field: "destination ids"). I am sure there used to be this option in the docs but i cannot find it anymore eg:
{% set links = ez_field_value( footer, "first_links_row" ).destinationContentIds%}
{% for id in links %}
{{ render(controller("ez_content:viewAction", {"contentId": id, "template": "link.html.twig"})) }}
{% endfor %}
Where the link.html.twig would simple print the link:
<a href="{{ path( "ez_urlalias", {"contentId": id} ) }}">
{{ ez_field_value( content, "name" ) }}
</a>
If using a custom tpl is not possible with the render (controller ( helper then a new custom view type would also fix this issue, but i cannot find documentation for either.
You can create a twig function that would do that. We have something like this:
Definition:
new Twig_SimpleFunction(
'content_name',
array($this, 'getContentName')
),
Implementation:
public function getContentName($content, $forcedLanguage = null)
{
if (!$content instanceof Content && !$content instanceof ContentInfo) {
$contentInfo = $this->repository->getContentService()->loadContentInfo($content);
} elseif ($content instanceof Content) {
$contentInfo = $content->contentInfo;
} else {
$contentInfo = $content;
}
return $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, $forcedLanguage);
}
which enables you to provide either content id, content info or content itself, and it returns translated content name
I'm working with Symfony and I want to add menu bar to my site. I have a simple menu bar with 3 links that are static and always be the same like "Home" but I need to add new links that should be dynamic. I have my admin panel where I can add new pages (info about them is stored in database - title, content etc.) and what I want to achieve is that after adding a new page, a link to this page should appear in this menu. I have this menu in my header.html.twig file which I include to every page same as footer.
Best of what I was able to achieve at that moment:
DefaultController.php
public function headerAction ()
{
$news = $this->getDoctrine()
->getManager()
->createQueryBuilder()
->from('AppBundle:News', 'n')
->select('n')
->where('n.type = :news')
->setParameter('news', 'other')
->getQuery()
->getResult();
$parsedown = new Parsedown();
foreach($news as $key => $new){
$news[$key]->setContent($parsedown->text($new->getContent()));
}
return $this->render('default/other_menu.html.twig', array(
'news' => $news
));
}
header-menu.html.twig
{# some bootstrap #}
{# simple static links #}
{{ render(controller('AppBundle:Default:header')) }}
{# simple static links #}
{# some bootstrap #}
other_menu.html.twig
{% for new in news %}
{{ new.title }}
{% endfor %}
But this solution works only when I have one page in database. With more of them - one long link is created (page1page2page3)
Any help will be appreciated.
I installed Userfrosting on the same hosting where I have Prestashop installed also. Userfrosting uses the same database as the Prestashop.
I want to create a page where the users registered on Userfrosting can review their sales on my ecommerce. (Prestashop supplier = userfrosting user)
How can I make a custom SQL query on that page? I found a query where I can filter sales by suppliers on prestashop, but don't know how to implement it with UserFrosting (it uses html pages).
Although I dont recommend this method, but this a simple way to query the table and show information on a page without getting into Userfrostings MVC.
In index.php just below feature pages comment use this code to register a page having a url as /mysales, it also fetches user information from the user_sales table and renders mysales.twig to show the information.
$app->get('/mysales/?', function () use ($app) {
// Access-controlled page
if (!$app->user->checkAccess('uri_dashboard')){
$app->notFound();
}
$db_config = $app->config('db');
$db_mysqli = new mysqli($db_config['db_host'], $db_config['db_user'], $db_config['db_pass'], $db_config['db_name']);
$sales_rows = array();
$result = $db_mysqli->query('select `description`, `total` from `user_sales` where `user_id` = 10 ');
if($result){
while($row = $result->fetch_assoc()) {
$sales_rows[] = $row;
}
}
$app->render('mysales.twig', [ //calls to render mysales.twig
'sales_rows' => $sales_rows //passing variables to twig
]);
});
Now create mysales.twig inside userfrosting/templates/themes/default folder having the code to display content from sales_rows twig variable. This will extend the dashboard-layout.twig so that the navigation panels stay in place.
{% extends "layouts/layout-dashboard.twig" %}
{% set page_group = "dashboard" %}
{% block page %}
{% set page = page | merge({
"title" : "My Sales",
"description" : ""
}) %}
{{ parent() }}
{% endblock %}
{% block content %}
<h1>My Sales</h1>
<table border="1">
<tbody>
<tr>
<td><strong>Description </strong></td>
<td><strong>Total </strong></td>
</tr>
{% for sales in sales_rows %}
<tr><td>{{ sales.description }}</td><td>{{ sales.total }}</td></tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
thats it now if you log in and navigate to /mysales you should get the information from the table.
How could I achieve a form for each list item using csrf and validation in symfony way?
I have a Task entity, which has comments property with a relation OneToMany. So I want to list all tasks and include a hidden comment form for every task. Usually I pass generated forms from controller to template, but how to create them dinamically in template?
{% for task in tasks %}
<taskinfo>
<task comment form>
{% endfor %}
Solved using this way:
In controller:
$forms = array();
foreach($tasks as $task) {
$entity = new TaskComment();
$forms[$task -> getId()] = $this ->createTaskCommentForm($entity, $task -> getId());
}
return $this->render('Bundle:blah:index.html.twig', array(
....
'forms' => $forms
));
An then comment box near every Task box in view:
...task info...
{% for task in tasks %}
<div class="comment-box">
{{ form(forms[task.id]) }}
</div>
{% endfor %}
P.S. I'm using collapsible panels to show/hide each task.
Maybe you need to embed a collection of forms? If so, here and here you can read more.
SETUP:
Twig 1.13.1
PHP 5.4.3
PROBLEM:
I am needing help setting up a custom tag that calls a function that i have already built...
Current Code:
Template Code
{% set stories = get_latest_stories(2, sports) %}
{% for story in stories %}
{{ story.headline }} <br>
{% endfor %}
Controller
$function = new Twig_SimpleFunction('getViewStories', function (section, limit) {
return news_stories::getStories(section,limit);
});
$twig->addFunction($function);
$twig->render("storyList.html");
GOAL:
No with that said I would like to use a custom tag like
{% get_latest_stories 2 sports %}
to call the same function as above. The new way looks nicer and is easier to follow
Why not fetch your stories in the controller instead of the template? This does not seem like a job for the view layer...
So, something like this:
$twig->render("storyList.html", array(
'stories' => news_stories::getStories($section, $limit)
));
Then, you'll have a stories variable available in your template.
here is simple example how to write twig extension
Following code is taken from my unfinished project
function file_import($value){
//some code here
return $value;
}
$app['twig']->addFunction('file_import', new Twig_Function_Function('file_import'));
usage
{{ file_import('value') }}