how to implement two layer of layout in Phalcon? - phalcon

I'm using Phalcon and Volt ,
this is folder Structure in my project for layout
app/views/layouts/default.volt
and structure is like this
default.volt is main layout that come with all pages .
now I need another layer of Layout to come with all pages.
this structure would be like this
I think it's clear with these photos .
So, How I can do that ?

your "main layout":
app/views/[controller].volt
your "another layout":
app/views/layouts/[controller].volt
your "page content":
app/views/[controller]/[action].volt

You can write in your views/index.volt:
{% include 'layouts/' ~ config.view.theme ~ '/header' %}
{% include 'layouts/' ~ config.view.theme ~ '/body' %}
{% include 'layouts/' ~ config.view.theme ~ '/footer' %}
So, you can manage your theme from your controller base like:
function initialize()
{
$controller = $this->dispatcher->getControllerName();
switch ($controller) {
case 'controller1':
case 'controller2':
$this->config->view->theme = 'mytheme_with_menu';
break;
case 'controller3':
case 'controller4':
$this->config->view->theme = 'mytheme_without_menu';
break;
}
}

Related

How Can I use operator ternary in phalcon version 3.4 in volt?

I would like to use the phalcon ternary operator provided by version 3.4, but I would like to use it with the one validation at the same time.
{% set pageType = 'basicSetting' %} {{ pageType == 'basicSetting' ? 'class="active"' : '' }}
The above example is from
here
After some research and analysis, I was able to find this,it is a little example
{% set result_that_you_want = (value==1) ? 'the result is 1' : 'the result is not the same the 1' %}

Django - Parameter passed to the template but it can not use in if statement

I try to pass the variable to another page using GET method in django. It is possible for me to do that, but the problem is that the variable that I passed is not available in the if statement. I try to print out the value then it worked fine. Then I try to use it inside if statement then I come to know that it was not working properly. I have no idea regarding that. Can anyone help me? Thank you very much.
This is my views:
def test(request):
Test = Photos.objects.all()
ID = request.GET['id']
Context = {
'ID' : ID,
'test' : Test,
'testing' : 3,
}
return render(request, 'test.html', Context)
def tests(request):
tests = Photos.objects.all()
Context = {
'tests' : tests,
}
return render(request, 'tests.html', Context)
This is my urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'flashapp.views.home'),
url(r'^play$', 'flashapp.views.play'),
url(r'^test$', 'flashapp.views.test'),
url(r'^tests$', 'flashapp.views.tests'),]
In tests.html I have buttons for passing id to test.html using GET method.
This is tests.html:
</head>
<body>
<h1>Welcome to tests pages.....</h1>
{% for i in tests %}
Click For {{ i.id }}<br>
{% endfor %}
</body>
This is test.html:
<html>
<head>
</head>
<body>
<h1>Welcome to Test Page</h1>
<h1>{{ ID }}</h1>
{% for i in test %}
<p>{{ i.id }}..........{{ testing }}.........{{ ID }}</p>
{% if ID == i.id %}
<p>Test</p>
<p>Working....ID = {{ i.id }}</p>
{% else %}
<p>In else</p>
{% endif %}
{% endfor %}
</body>
this is tests.html
this is test.html
I suppose to see the "Working....." but, it gone to else block. I have no idea. Help me please!!!
Thank you very much.
I edited my original answer based on your comment
You are passing a QuerySet as your Test object instead of an object in the first view, so it doesn't have an id property.
When you do this:
def test(request):
Test = Photos.objects.all()
...
You are getting a collection of all the Photos objects into the Test variable, which is not what you want, you only want one instance of Photos. For that kind of queries, you need to use the .get method, that returns a single instance or an exception in case it doesn't find it.
Test = Photos.objects.get(pk=request.GET['id'])
Your code now should look like this:
def test(request):
ID = request.GET['id']
Test = Photos.objects.get(pk=ID)
Context = {
'ID' : ID,
'test' : Test,
'testing' : 3,
}
return render(request, 'test.html', Context)
Now, for completeness' sake, this would fail in case the ID is not on the database, so we can do something like this:
def test(request):
try:
ID = request.GET['id']
Test = Photos.objects.get(pk=ID)
Context = {
'ID' : ID,
'test' : Test,
'testing' : 3,
}
return render(request, 'test.html', Context)
except Photos.DoesNotExist:
raise Http404("No Photo matches the given query.")
Of course, Django has its own shortcuts for these kind of things, so your code can be written like this:
from django.shortcuts import get_object_or_404
def test(request):
#I strongly suggest you don't use uppercase in variable names
id = request.GET['id']
test = get_object_or_404(pk=id)
context = {
'ID' : id,
'test' : test,
'testing' : 3,
}
return render(request, 'test.html', context)

How can I use literal list in Django template file?

From a Django template file I want to check if my variable myVar is "A" or "B" (or "C" or "D", etc..).
So I would like to do something like this:
{% if myVar in ["A", "B"] %}
Hello
{% else %}
World
{% endif %}
But this gives me a syntax error in the first line.
How then can I use a literal list in the Django template file?
you can't define a list directly in the template. You could pass a list to the template using a view in views. py and a url in urls. py
ex:In views. py
def view_name(request):
list_name=['A', 'B', 'C'];
render(request, 'directory/template_name.html'
{'list_name':list_name})
In urls. py
urlpatterns = [
path('index/', views.view_name, name='main-view'),
]
In template
{% if myVar in list_name%}
Hello
{% else %}
World
{% endif %}
The answer can be found here: check for presence in a list django template
This question is essentially a duplicate.

How to get all products of a Shopify shop?

In my theme development, I don't find the way to get all the products of my shop.
Although, I can retrieve all the collections with the variable collections (exemple: {% for c in collections %}).
Check this url: https://help.shopify.com/en/themes/customization/collections/change-catalog-page
Like magic... all your products...
Get all products at once or to run a query(API Request) for all products in shopify store :
using this app is more managed -> https://github.com/phpish/shopify_private_app-skeleton so, my solution below is based on this app or you can relate the solution with your solution as well
<?php
session_start();
require __DIR__.'/vendor/autoload.php';
use phpish\shopify;
require __DIR__.'/conf.php';
$shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true);
try
{
$products = $shopify('GET /admin/products/count.json', array('published_status'=>'published'));
$totalproducts = $shopify('GET /admin/products/count.json', array('published_status'=>'published'));
$limit = 50;
$totalpage = ceil($totalproducts/$limit);
for($i=1; $i<=$totalpage; $i++){
$products = $shopify('GET /admin/products.json?'.$limit.'=50&page='.$i, array('published_status'=>'published'));
foreach($products as $product){
//do anything at once for all the products in store
}
}
}
catch (shopify\ApiException $e)
{
//
}
Summary : The idea is to retrieve with page=x as parameter. after calculating the number of pages we will have with specified limit i.e 50 at one time fetch.

using Liquid variables inside of a liquid tag call

I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so
{{ assign id = 'something' }} // this value is actual dynamic while looping through data
{% link_to article: id, text: 'Click Me!' %} // my custom tag
However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it.
Does anyone know how to pass variables into tag calls?
I've recently solved this very simply with Jekyll 0.11.2 and Liquid 2.3.0 by passing the name of the variable as the tag parameter.
{% assign v = 'art' %}
{% link_to_article v %}
You can also pass the name of the control var while in a loop, like article above.
In Liquid::Tag.initialize, #markup is the second parameter, the string following the tag name. The assigned variables are available in the top level of the context.
def render(context)
"/#{context[#markup.strip]}/"
end
This obviously only allows one param to be passed. A more complex solution would parse params like x: 2, y: 3.
This solved the case for me context[#markup.strip].
My problem was that i wanted to be able to pass a variable to my custom Liquid tag like this: {% get_menu main_menu navigation.html settings.theme.id %}
In order to do this i first split the variable string into different varaibles on every space character.
class GetMenu < Liquid::Tag
include ApplicationHelper
def initialize(tag_name, variables, tokens)
#variables = variables.split(" ")
#menu_object = #variables[0]
#file_name = #variables[1]
#theme_id = #variables[2]
super
end
def render(context)
# This is where i use context[#theme_id.strip] to get the variable of "settings.theme.id"
content = CodeFile.find_by(hierarchy: 'snippet', name: #file_name.to_s, theme_id: context[#theme_id.strip])
#menu ||= Menu.find_by_slug(#menu_object)
context.merge('menu' => #menu)
Liquid::Template.parse(content.code).render(context)
end
end
Liquid::Template.register_tag('get_menu', GetMenu)
*This is just a more rich example that the answer above by Jonathan Julian
Doesn't look like this is possible, my solution was to just pass the variable name in to the tag and grab it out of the context the tag is being rendered in. Like so:
{% for article in category.articles %}
{% link_to variable: article, text: title %}
{% endfor %}
in my tag code (condensed):
def render(context)
uri = "article/#{context[#options[:variable]]['id']}"
"<a href='#{uri}'>#{build_link_text context}</a>"
end
It would be great to have a tag that can be called with literals and variables like
{% assign v = 'art' %}
{% link_to_article v %}
or
{% link_to_article 'art' %}
or
{% link_to_article "art" %}
and also of course
{% link_to_article include.article %}
In order to so I propose a helper function
def get_value(context, expression)
if (expression[0]=='"' and expression[-1]=='"') or (expression[0]=="'" and expression[-1]=="'")
# it is a literal
return expression[1..-2]
else
# it is a variable
lookup_path = expression.split('.')
result = context
puts lookup_path
lookup_path.each do |variable|
result = result[variable] if result
end
return result
end
end
And in the render just call the helper function to get the value of the literal or variable.
def render(context)
v = get_value(context, #markup.strip)
end
FYI, the initialiser would look like this:
def initialize(tag_name, markup, tokens)
#markup = markup
super
end
This does not strictly answer the question, but it may help others who are new to Liquid (like myself) and try something like this. Instead of implementing a custom tag, consider implementing a custom filter instead. Variables are resolved before they are passed into filters.
Ruby code:
module MyFilters
def link_to_article(input, text)
"<a href='https://example.org/article/#{input}'>#{text}</a>"
end
end
Liquid::Template.register_filter(MyFilters)
Liquid template:
{% assign id = 'something' %}
{{ id | link_to_article: 'Click Me!' }}
Output:
<a href='https://example.org/article/something'>Click Me!</a>
You can also use variables as parameters. So the following would have the same output:
{% assign id = 'something' %}
{% assign text = 'Click Me!' %}
{{ id | link_to_article: text }}
And filters can have zero or more (comma-separated) parameters:
{{ 'input' | filter_with_zero_parameters }}
{{ 'input' | filter_with_two_parameters: 'parameter 1', 'parameter 2' }}