我遇到的问题是页面上的各种翻译使用 URL 参数来动态填充表单的内容。该表单有单选按钮,我希望根据 URL 参数默认选中其中一个按钮。
例如,如果 URL 为 ?lang=dk
,我希望选中欧洲单选按钮,但如果它为 ?lang=fr-ca
>,我希望它检查北美:
<form id="contact-form" method="post" class="form-contact" enctype="multipart/form-data">
<div id="form-platform" class="form-platform--radio-group">
<label class="radio--button" for="option-na">
<input class="form__input" type="radio" id="option-na" name="platform"
value="NA">
<i></i>
<span class="radio--label">North America</span>
</label>
<label class="radio--button" for="option-eu">
<input class="form__input" type="radio" id="option-eu" name="platform"
value="EU">
<i></i>
<span class="radio--label">Europe</span>
</label>
</div>
</form>
到目前为止,我已经尝试使用 this.param
但到目前为止还没有成功。
我唯一的选择是使用 Javascript 来实现我的需求,还是可以使用 OctoberCMS 实现这一点?
最佳答案
澄清:
像“example.com?lang=us”这样的url正在服务器上发出请求。请求为Input::get('lang') = 'us'
。
像“example.com/:lang?/”这样的 url 正在使用 Laravel 路由系统来访问 slug 的参数。这看起来像“example.com/us/page”,请求看起来像 this.param('lang') = 'us'
。
这两种方法都适用于您的系统,具体取决于您设置语言的方式。
我的建议是在设计网站时考虑到动态值并使用这样的集合。
CMS 页面中的 PHP 代码用于临时:
function onStart() {
$language = Input::get('lang'); //check for a language request
$languages = [ //build language array
'dk' => [
'place' => 'European',
'selected' => false,
'value' => 'eu'
],
'fr-ca' => [
'place' => 'North America',
'selected' => false,
'value' => 'na'
]
];
if (isset($languages[$language])) {
$languages[$language]['selected'] = true; //see if the language request is in the array then set selected to true
}
$this['languages'] = $languages; //return languages to the page
}
在页面的标记部分,您可以执行此操作 - 使用 twig,您只需创建一个 for 循环即可设置变量并检查是否选择了语言。
<form id="contact-form" method="post" class="form-contact" enctype="multipart/form-data">
<div id="form-platform" class="form-platform--radio-group">
{% for language in languages %}
<label class="radio--button" for="option-{{ language.value}}">
<input class="form__input" type="radio" id="option-{{ language.value }}" name="platform"
value="{{ language.value | upper }}" {% if language.selected %}checked{% endif %} >
<i></i>
<span class="radio--label">{{ language.place }}</span>
</label>
{% endfor %}
</div>
</form>
关于javascript - 是否可以在 OctoberCMS 的 If 语句中使用 URL 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265441/