PHP. Write an anchor in the Smarty template. (Kohana 3 + KSmarty) - kohana

I'm learning Kohana 3.2.0 together with KSmarty for Kohana 3. I'd like to write an anchor on the page like this:
Page list
I can build the url in the controller and pass it to Smarty as a variable but. Is there a way to build the anchor or URL in Smarty template (including "http://www.mysite.cz" part)?
If it is not possible to build the anchor. Is it at least possible to build full URL?
The Reason: I have a main template which includes another template.
The main template will be used by multiple controllers and I would like to avoid building the URL in each controller. Therefore I'll be happy if KSmarty will be able to do it for me.

The only solution I have found is to write custom function. Save following code into function.url.php file in Smarty plugins directory:
function smarty_function_url($params, &$smarty)
{
$type = '';
if(isset($params['type'])) $type = $params['type'];
$protocol = 'http';
if(isset($params['protocol'])) $protocol = $params['protocol'];
$url = '';
if(isset($params['url'])) $url = $params['url'];
$text = '';
if(isset($params['text'])) $text = $params['text'];
switch($params['type'])
{
case 'url':
return Kohana_URL::site($url, $protocol);
case 'anchor':
$url = Kohana_URL::site($url, $protocol);
return "<a href='{$url}'>{$text}</a>";
default:
return Kohana_URL::base('http');
}
}
Examples of use in Smarty template:
{url}
{url type='url' url='admin/categories' protocol='https'}
{url type='anchor' url='admin/articles' text='List of articles'}
The first block in which variables are set I had to write otherwise Smarty was generating notice "Undefined variable...". I'm just PHP student, suggestions for code improvement are welcome.
Hope it will help the others.

Related

Passing JavaScript variable into snippet

I'm working on a search form for my ModX application that is consisted of a chunk and a snippet. What I'm trying to achieve is to pass what was entered into the search box into a javascript variable and then pass it to my snippet, however, the snippet receives the literal text, and not the value that I enter into the parameter when I call it.
I don't know if what I'm attempting is possible in ModX or if I need to take a different approach, but I would be hugely thankful for anyone who can provide any insight.
Chunk:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
Snippet:
<?php
$search = $modx->getOption('q', $scriptProperties);
echo $search; // this always prints "search"
?>
I doubt that this code makes sense:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
The snippet call returns the result of snippet's execution with param q always equal to the string 'search' in your case and finally on your page you will have something like this:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
'search' // assuming your snippet just returns what has been passed to it.
});
</script>
In order to accomplish your task you can use a simple trick. Call your snippet like this:
[[!yourSnippet? &yourVar=`[[!#POST.yourVar]]` ]] // or GET
Lets say this snippet call is located on a page accessible via url /test/ on your server. So, now you just have to send the parameters you collected from your search form using AJAX to the /test/ page where your snippet is:
var yourVar = $('.search-entry').val();
$.ajax({
type: "POST",
url: "/test/",
data: {yourVar: yourVar},
success: success,
dataType: "html"
});
Hope it helps :)
PS If you want to search Resource content and TV content, I can highly recommend an extra called SimpleSearch.

Check URL exists in ApplicationContentUriRules

I have an app that is loading URLs into a WebView (x-ms-webview). When the user makes the request, I would like to compare the URL they are trying to load with the "whitelisted" URLs in the ApplicationContentUriRules and warn them if it is not there. Any ideas how I might accomplish this?
There isn't a direct API for pulling information out of the manifest, but there are options.
First, you can just maintain an array of those same URIs in your code, because to change them you'd have to change the manifest and update your package anyway, so you would update the array to match. This would make it easy to check, but increase code maintenance.
Such an array would also let you create a UI in which the user can enter only URIs that will work, e.g. you can offer possibilities from a drop-down list instead of letting the user enter anything.
Second, you can read the manifest XML into a document directly and parse through it to get to the rules. Here's some code that will do that:
var uri = new Windows.Foundation.Uri("ms-appx:///appxmanifest.xml");
var doc;
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file);
}).done(function (text) {
doc = new Windows.Data.Xml.Dom.XmlDocument();
doc.loadXml(text);
var acur = doc.getElementsByTagName("ApplicationContentUriRules");
if (acur !== null) {
var rules = acur[0].getElementsByTagName("Rule");
for (var i = 0; i < rules.length; i++) {
console.log(rules[i].getAttribute("Match") + " - " + rules[i].getAttribute("Type"));
}
}
});
You could probably just get "Rule" tags directly from the root doc, because I don't think anything else in the manifest uses that kind of node, but to future-proof it's better to get the ApplicationContentUriRules first. As you can see, the "Match" attribute is what holds the URI for that rule, but you also need to make sure that "Type" is "include" and not "exclude".

Generating query strings in Laravel 4

I have a named route
Route::get('login/facebook', array(
'as' > 'login.facebook.authorise',
'uses' => 'AuthController#getFBAuthorise'
));
and want to produce a URL pointing to it with a query string appended.
For example:
URL::route('login.facebook.authorise', array("next"=>'/dashboard/')
would produce
/login/facebook?next=/dashboard/
Is something like this built in to the framework? If not, what is the best way to try and do this?
Thanks.
The URL helper can do this.
http://laravel.com/docs/helpers#urls
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());
EDIT: To get only the URL :
http://laravel.com/docs/routing#named-routes
$url = URL::route('route.name', $parameters);
I am a bit late to the party
Just dove into the classes and methods for building urls and you can do this:
link_to_route('login.facebook.authorise','Facebook Login',array('next'=>'/Dashboard/'))
So. the array will always fallback to query params unless the names are declared in the route.
If the use case for this is redirecting to a page after logging in then you could use
Redirect::intended('dashboard');
As seen here http://laravel.com/docs/security#authenticating-users
Sorry if that's not what you're trying to achieve.

MODx caches what it shouldn't

I'm working with MODx revo. I wrote a snippet called putBoxId with the following content:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
if (!is_null($id)) {
return $modx->getChunk($tpl, array('id' => $id));
}
return '';
I use it like this: [[~3[[!putBoxId? &tpl='boxIdUrl']] ]] (with backticks, of course), where boxIdUrl is the chunk with the following content:
? &id=`[[+id]]`
The problem is, for some reason it gets cached. I tried putting '!' In all combinations, still gets cached. How can this be fixed?
The [[~3 is being cached, so your putBoxId is actually called only the first time.
In Revo - any *[[* (tag) can start with a ! (non-cacheable flag). So, in your case - [[!~3[[!putBoxId? &tpl='boxIdUrl']] ]] (note: there's a typo here and in your original question, see comment below. this should work: [[~3]][[!putBoxId? &tpl='boxIdUrl']])
more info here
Even better - unless there's a good reason, get rid of that chunk, as the $modx->getChunk call wouldn't be cached in your scenario (goes to db to get template, etc...).
Do it all in the snippet itself using modx->makeUrl (see link for more options)
<?php
$resourceId = $modx->getOption('resourceId', $properties, $modx->resource->get('id')); // get resourceId from snippet, default to current
$args = (!empty($_REQUEST['id']))? array('id'=>$_REQUEST['id']) : '';
return $modx->makeUrl($resourceId, '', $args);
Call like this:
[[!putBoxId]] or [[!putBoxId? &resourceId=`3`]]

Drupal autocomplete fails to pull out data as a subdomain

I managed to pull out data using autocomplete at my local (http://mysite.dev/swan/autocomplete). The json data is displayed.
But when I applied the same module at live (now a subdomain: http://test.mysite.com/swan/autocomplete with different drupal installs), this autocomplete fails to pull out data. No json data is displayed.
Do you have any idea if this is related to cross domain issue, or any possible cause I might not be aware of?
This is the callback:
/**
* Callback to allow autocomplete of organisation profile text fields.
*/
function swan_autocomplete($string) {
$matches = array();
$result = db_query("SELECT nid, title FROM {node} WHERE status = 1 AND type='organisation' AND title LIKE LOWER ('%s%%')", $string, 0, 40);
while ($obj = db_fetch_object($result)) {
$title = check_plain($obj->title);
//$matches[$obj->nid] = $title;
$matches[$title] = $title;
}
//drupal_json($matches); // fails at safari for first timers
print drupal_to_js($matches);
exit();
}
Any hint would be very much appreciated.
Thanks
It's the conflict with password_policy.module. Other similar modules just do the same blocking. These modules stop any autocomplete query.

Resources