HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

DuckDuckGo Smarty Template Function to replace {searchbox}

I've come up with a PHP Smarty template function to display a DuckDuckGo search function. I put this in my template, where it's used.

To make this modular and share-able with other forum admins, where would y'all suggest I put this? It's a bit too little to warrant a plugin IMHO.

The code:

function smarty_function_duckduckgo($params, &$smarty) {
    /// Search text field placeholder value.
    $placeholder = array_key_exists('placeholder', $params) ? val('placeholder', $params, '', true) : t('SearchBoxPlaceHolder', 'Search');
    /// DuckDuckGo site-search value.
    $site = array_key_exists('site', $params) ? val('site', $params, '', true) : 'zettelkasten.de';

    $form = Gdn::factory('Form');
    $result = $form->open(['action' => 'https://duckduckgo.com/?kp=-1&kl=us-en&kg=g', 'method' => 'get', 'accept-charset' => 'utf-8', 'role' => 'search', aria-label => 'Sitewide']).
            $form->hidden('sites', [ 'value' => $site]).
            $form->textBox('q', ['placeholder' => $placeholder, 'name' => 'q', 'spellcheck' => 'false', 'accesskey' => '/', 'aria-label' => t('Enter your search term.'), 'title' => t('Enter your search term.'), 'role' => 'searchbox', 'class' => 'InputBox js-search']).
            $form->button('Go', ['name' => '', 'arial-label' => t('Search')]).
            $form->close();
    return $result;
}

You can then use it like so: {duckduckgo site='my.site.com' placeholder='Search My Site'}

Cheers,

Christian

Comments

  • There's a tutorial category where I would see snippets like that. Shall I move this discussion to that category?

  • K17K17 ✭✭✭

    Hi :D thanks for sharing :D

  • @K17 Happy to help! :)

    @R_J Sure, if you think that's a good fit! I also blogged a bit about the approach to provide more context for noobs like me: https://christiantietze.de/posts/2021/01/vanilla-forum-duckduckgo-search/

    Implicitly, this sounds like "a tutorial is good enough" and there's no need to make this shippable as a plugin, right?

  • In theory it should be possible to override the normal search box template with yours in a plugin

    If you can achieve that, it would make a good plugin. Otherwise I wouldn't know how you should "package" it to a plugin

Sign In or Register to comment.