Shopify Assets API unexpected constant in blade template - laravel-5

I am passing mySnippet in Shopify via assets API then later i want to use the variables passed from include snippet i am getting unexpected constant
Snippet code i put in cart.liquid file
{% include 'mySnippet', appKey:'2001' %}
My PHP CODE
$mySnipet = view('mySnippet')->render();
Blade file
{{mapKey}}
Error
Unexpected constant mapKey

You can pass liquid variable string in value of key in view then from your snippet you can use it as following
{% include 'mySnippet', appKey:'2001' %}
PHP CODE
$data = array();
$data['mapKey'] = "{{mapKey}}";
$mySnippet = view('mySnippet',$data)->render();
Blade Template
{{$mapKey}}
Final output in template.liquid at Shopify will be following
{{mapKey}}
Once your snippet render on Shopify page it
2001

Related

Can I use a Vue interpolation inside a VuePress code block

In my VuePress project I have a code block that looks like this
```cs
string pageName = getPageName("{{ $page.key }}");
```
What I want to see on the static site is:
```cs
string pageName = getPageName("v-10013ae8");
```
Where the interpolation of {{ $page.key }} is rendered as v-10013ae8
I've tried
~~~{{ $page.key }}~~~
````{{ $page.key }}````
This is the VuePress documentation I have been following
Templating/Interpolation
Syntax Highlighting

laravel 5.2 carousel only in index page doesn't wok

I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')

October CMS and navigation on current page

I am trying to get the basically the active class applied to the current page. As it goes, the builder plugin is setting the URL through:
<a href="{{ detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }) }}">
However I am new to October so I am not sure how to reference this.page.id in comparison to the url set above.
Basically I want this:
{ set UrlParam = detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }
{% if this.page.id == UrlParam %} class="active" {% endif %}
Any ideas?
One of the best debugging plugins out there for OctoberCMS is this: https://octobercms.com/plugin/davask-dump
That plugin makes connecting your twig templates to your database / php rendering a breeze.
After installing that plugin you can use {{ d(variable) }} instead of {{ dd(variable) }} and get more information on the array nests etc.
So I would do {{ d(UrlParam) }} and {{ d(this.page.id) }} in your twig template. See what the dump has to say about each of those variables. For clarity I do believe you need the % here {**%** set 'variable' **%**}.
I am also not a fan of the builder component and I use the PHP section on the page / partial pages. And establishing a class with the use function and access the data with $this['variable']. Maybe worth looking into here is a quick example:
Pluginauthor\Plugin\Models\Plugin;
function onStart() {
$plugin = Pluggin::all();
$this['plugin'] = $plugin;
}

How to read variables from locals in Swig custom tag/filter?

I'm trying to build i18n module for my app. Since I'm using swig I would prefer to create custom tag "trans" for that so translations result could be cached.
I'm using Express.js 4 as base for my app.
How can I read variable from request inside custom tag or filter? I have no idea how to read them (especially inside filters). I can't even access variables I pass to template. For example:
swig template index.swig
-------------------------------
{% trans %}this is sparta{% endtrans %}
{% trans "fr" %}this is sparta{% endtrans %}
-------------------------------
I can make line 2 works. I pass locale for "trans" and in my code replace text passed by user from a French translation.
But I don't want to pass locale to each and every trans tag. I would like to do something like that:
function (req, res, next) {
res.locals.locale = req.session.user.getLocale();
}
// now view should have locale variable
// and trans should use it to pick right translation from the table
{% trans %}this is sparta{% endtrans %}
Can anyone provide a tutorial or an explanation? Documentation for that part of swig is non existent. The problem is that each user can have different language so lang code is picked per request and I should have access to that inside swig custom tag.
The template automatically reads locals from both app.local and res.local. Therefore, as noted, you could add middleware to set the locals on the response as follows:
app.use(function (req,res,next) {
res.locals.locale = req.session.user.getLocale();
next();
});
This would expose locale as a global inside of your view.

Laravel, variable output work with View::share but not with VIew::composer

I need the same menu in all my views.
So I get data I need for output the menu in a constructor defined in my BaseController.
To got the data I've first tried to use View::composer but dunno why I doesn't get any error, it looks like View::composer isn't executed at all...
If I use View::share, its work
//BaseController.php
//function called in the constructor
public function init()
{
$envs = $this->game->environments()->get();
View::share('test', $envs);
View::composer('layouts.base', function($view)
{
$view->with('envs', $envs);
});
}
//base.twig
//nothing output here, no error
{% for env in envs %}
{{ env.name }}
{% endfor %}
//its work
{% for env in test %}
{{ env.name }}
{% endfor %}
I'm new to laravel so maybe I miss something ?
I think the reason that this isn't working is because of where you put the View Composer in your code. View Composers in Laravel are essentially callbacks that are executed as soon as the view is rendered. Where is this init() function defined? My guess is that the view is being created before your view composer is defined - meaning that the view composer callback will never be executed.
Try moving the whole view composer block from the init() function and append it to the bottom of your routes.php file and see if it works. That's not a bad spot to place your view composers if you do not have too many, if you do you could create a new class to store them in and add that path to your autoload path.
Read more about view composers here

Resources