Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Mar 2017
    Posts
    2,195

    Detect user-agent in template?

    Is it possible to detect the user's browser's user-agent in a skin template? For Material I have a section in index.html as follows:

    Code:
      [% PERL %]
      my $version=Plugins::MaterialSkin::Plugin->pluginVersion();
      print("<link href=\"html/css/themes/dark.css?r=$version\" rel=\"stylesheet\" id=\"themecss\">\n");
      print("<link href=\"html/css/variant/standard.css?r=$version\" rel=\"stylesheet\" id=\"variantcss\">\n");
      print("  <link href=\"html/css/colors/blue.css?r=$version\" rel=\"stylesheet\" id=\"colorcss\">\n");
      print("  <link href=\"html/css/style.css?r=$version\" rel=\"stylesheet\">\n");
      print("  <link href=\"html/css/mobile.css?r=$version\" rel=\"stylesheet\" id=\"layoutcss\">");
      [% END %]
    I'd like something along the lines of:

    Code:
      [% PERL %]
      my $version=Plugins::MaterialSkin::Plugin->pluginVersion();
      my $ua = $request->header('user-agent');
      if (index($ua, 'Android') != -1) {
          print("<link href=\"html/css/themes/darker.css?r=$version\" rel=\"stylesheet\" id=\"themecss\">\n");
      } else {
          print("<link href=\"html/css/themes/dark.css?r=$version\" rel=\"stylesheet\" id=\"themecss\">\n");
      }
      print("<link href=\"html/css/variant/standard.css?r=$version\" rel=\"stylesheet\" id=\"variantcss\">\n");
      print("  <link href=\"html/css/colors/blue.css?r=$version\" rel=\"stylesheet\" id=\"colorcss\">\n");
      print("  <link href=\"html/css/style.css?r=$version\" rel=\"stylesheet\">\n");
      print("  <link href=\"html/css/mobile.css?r=$version\" rel=\"stylesheet\" id=\"layoutcss\">");
      [% END %]
    ...but I don't know how to determine the user-agent.
    Material debug: 1. Launch via http: //SERVER:9000/material/?debug=json (Use http: //SERVER:9000/material/?debug=json,cometd to also see update messages, e.g. play queue) 2. Open browser's developer tools 3. Open console tab in developer tools 4. REQ/RESP messages sent to/from LMS will be logged here.

  2. #2
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    20,460

    Detect user-agent in template?

    > [% PERL %]
    > my $version=Plugins::MaterialSkin::Plugin->pluginVersion();
    > print("<link href="html/css/themes/dark.css?r=$version" rel="stylesheet" id="themecss">\n");
    > print("<link href="html/css/variant/standard.css?r=$version" rel="stylesheet" id="variantcss">\n");
    > print(" <link href="html/css/colors/blue.css?r=$version" rel="stylesheet" id="colorcss">\n");
    > print(" <link href="html/css/style.css?r=$version" rel="stylesheet">\n");
    > print(" <link href="html/css/mobile.css?r=$version" rel="stylesheet" id="layoutcss">");
    > [% END %]


    I hardly ever do (if at all) raw Perl in templates. But [% userAgent %]
    should be what you're looking for. Do you have a page handler for this
    template? Then I'd do something like

    $paramsRef->{version} = Plugins::MaterialSkin::Plugin->pluginVersion();

    and in the template just

    <link href="html/css/themes/dark.css?r=[% version %]" rel="stylesheet"
    id="themecss">


    --

    Michael

  3. #3
    Senior Member
    Join Date
    Mar 2017
    Posts
    2,195
    Quote Originally Posted by mherger View Post
    I hardly ever do (if at all) raw Perl in templates. But [% userAgent %]
    should be what you're looking for.
    How do I access [% userAgent %] from the perl code? Or would I need [% IF indexof(userAgent)!=-1 %] ... [ %END%] ???

    Quote Originally Posted by mherger View Post
    Do you have a page handler for this template?
    No, because its index.hml - I asked before, and I could not register a handler for that file.
    Material debug: 1. Launch via http: //SERVER:9000/material/?debug=json (Use http: //SERVER:9000/material/?debug=json,cometd to also see update messages, e.g. play queue) 2. Open browser's developer tools 3. Open console tab in developer tools 4. REQ/RESP messages sent to/from LMS will be logged here.

  4. #4
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    20,460

    Detect user-agent in template?

    > How do I access [% userAgent %] from the perl code? Or would I need [%
    > IF indexof(userAgent)!=-1 %] ... [ %END%] ???


    I think you'd do

    $stash->get('userAgent');

    from within the template's PERL block. See
    http://www.template-toolkit.org/docs...l#section_PERL

    --

    Michael

  5. #5
    Senior Member
    Join Date
    Mar 2017
    Posts
    2,195
    Quote Originally Posted by mherger View Post
    > How do I access [% userAgent %] from the perl code? Or would I need [%
    > IF indexof(userAgent)!=-1 %] ... [ %END%] ???


    $stash->get('userAgent');
    Perfect! Thanks once again...
    Material debug: 1. Launch via http: //SERVER:9000/material/?debug=json (Use http: //SERVER:9000/material/?debug=json,cometd to also see update messages, e.g. play queue) 2. Open browser's developer tools 3. Open console tab in developer tools 4. REQ/RESP messages sent to/from LMS will be logged here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •