Accelerated Mobile Pages (AMP) #137
Replies: 4 comments 14 replies
-
In version 3.0.0, Hook::set('route', function($content, $path, $query, $hash) {
if (str_starts_with($path, '/amp/')) {
State::set('is.mobile', true);
return Hook::fire('route', [$content, substr($path, 4), $query, $hash]);
}
return $content;
}, 0); |
Beta Was this translation helpful? Give feedback.
-
kang, kode untuk amp yang route method, pada attribut class tag html menampilkan class is:error dan error:404, kalau pakai string query method gak ada class is:error dan error:404 kalau untuk |
Beta Was this translation helpful? Give feedback.
-
Kang, bagaimana caranya agar html diatas hanya muncul pada halaman selain path |
Beta Was this translation helpful? Give feedback.
-
These techniques are useful for those of you who want to make a mobile version of the page without having to sacrifice the existing desktop version of the page. You can have two versions of the page without having to make two web pages for each device separately.
One of the most popular mobile web page standards today is AMP, a web component framework to easily create user-first web experiences. Today, the AMP specifications may have changed, therefore I will not discuss it in detail here. You can learn the complete AMP specifications on the official website.
Here I just want to focus on the methods you can use to tell Mecha that it should display the mobile version of the page if the conditions are met.
Mobile Page Conditional Statement and Helper Functions
Query String Method
This is the easiest method that you can implement right away by adding a mobile page specific conditional statement based on the presence of a particular query string in URL. In this case, the query string in question is
?amp=1
The code above will add a mobile conditional feature that you can use in the layout file via the
$site
or$state
object like so:To activate the mobile version of the page, simply add an
?amp=1
query string in URL.When working with different web page mode, helper functions to convert URL from the default version to the mobile version and vice-versa may be needed. The following are two helper functions you can use to work with query string based mobile web page:
The following is an example of how to use it:
Domain Alias Method
Using a domain alias is also easy, but this requires you to hard-code the domain name in the source code:
The following are two helper functions you can use to work with domain based mobile web page:
Route Method
A little tricky, but still doable.
By adding these two route aliases, you will be able to have two types of URLs that will display the same page.
You can visit the page
http://127.0.0.1/article/lorem-ipsum
viahttp://127.0.0.1/amp/article/lorem-ipsum
too, and they will both display the same page. Until you state that the mobile page conditional statement is met before the actual route execution.The following are two helper functions you can use to work with route based mobile web page:
Hooking the URL, Globally
Applying helper functions to every URL inside the markup is a painstaking job. In addition, we also don’t seem to be able to do much with the URLs in the page content. By imitating the way link extension change URLs via the output buffer, we can make a simpler version of its hook to convert URLs only on
<a>
and<form>
elements.On a form of type
GET
, changing the action URL of the query string based mobile web page is useless.The following is an example of a search form markup:
Instead of appending query
?amp=1
to the action value, you can just add a hidden input element like this:Or, from the output buffer hook, you can insert the element like this:
It’s not neat actually, but it works!
Converting Native HTML Element into AMP Element
Beta Was this translation helpful? Give feedback.
All reactions