Answers.Body: <p>If you want to do this purely from a template, then something like this:</p>
  
  <pre><code>{% set sites = craft.app.sites.getAllSites() %}
  
  {% for site in sites %}
      &lt;li&gt;{{ site.name }}&lt;/li&gt;
  {% endfor %}
  </code></pre>
  
  <p>In Craft 3, <code>craft.app</code> in a template basically gives you access to everything in the <a href="https://github.com/craftcms/cms/tree/develop/src/services" rel="nofollow noreferrer"><code>services</code></a> namespace.  Adding <code>craft.app.sites</code> opens up all of the methods in <a href="https://github.com/craftcms/cms/blob/develop/src/services/Sites.php" rel="nofollow noreferrer"><code>services/Sites.php</code></a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-17T18:55:46.913Z
Answers.LastActivityDate: 2017-11-17T18:55:46.913Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Nikolay,</p>
  
  <p>As I responded in the slack, I will respond here in case anyone else has this question. </p>
  
  <p>In the template you can access this information like so:</p>
  
  <pre><code>SiteUrl: {{ siteUrl }}
  Langauge: {{ craft.app.language }}
  Handle: {{ craft.app.sites.currentSite.handle }}
  </code></pre>
  
  <p>I'm not entirely positive how to loop the sites in the template but you can get a sites object in PHP by running the following in a controller somewhere or in the <code>init()</code> method of any plugin that is installed:</p>
  
  <pre><code>$sites = Craft::$app-&gt;getSites();
  var_dump($sites);
  exit;
  </code></pre>
  
  <p>This will output the structure of the sites object. Baring this you can look through the craft source / docs to find the details on the site object that returns. There should be a method for getting sites by id or by handle; <code>$sites-&gt;getSitesByID()</code> or <code>$sites-&gt;getSitesByHandle()</code> but I have not double checked this so you'll have to try it or look it up. If there is not a method then it should be accessible via an array property like <code>$sites-&gt;sitesByHandle</code> or <code>$sites-&gt;byHandle</code>. Again you'll have to double check on this.</p>
  
  <p>By the way, that functionality should translate to the template version of the sites object <code>craft.app.sites</code> so you should be able to do:</p>
  
  <pre><code>{% set sites = craft.app.sites.getSitesByHandle() %}
  </code></pre>
  
  <p>Or something like that anyway.</p>
  
  <p>Once you figure it out feel free to update this to reflect the proper methods / properties :)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-17T02:27:07.690Z
Answers.LastActivityDate: 2017-11-17T02:27:07.690Z
Answers.UserDisplayName: Rick Kukiela
Answers.Body: <p>I created a script in order to save different credentials for each site in my google analytics plugin.
  So I had to render a dropdown to chose the site as well. Here is my code</p>
  
  <p>First the Plugin variable. You could actually remove the most and just return the <code>sites</code> variable</p>
  
  <pre><code>public function getSites(){
      $response = [];
      $sites = Craft::$app-&gt;sites-&gt;getAllSites();
      // I had some permission checks here as well to see if the user 
      // has access to the site, but I removed that in this example
      foreach($sites as $site){
          $response[] = array(
              "label" =&gt; $site-&gt;name,
              "value" =&gt; $site-&gt;id
          );
      }
      return $response;
  }
  </code></pre>
  
  <p>And this is my template.</p>
  
  <pre><code>{% set sites = craft.googleanalytics.getSites() %}
  {% set siteId = entry.lnkSiteId? entry.lnkSiteId : sites[0]["value"] %}
  {{ forms.selectField({
      label:          'Seite'|t,
      id:             'lnkSiteId',
      name:           'lnkSiteId',
      instructions:   'Bitte wählen Sie die Seite'|t,
      value:          siteId,
      options:        sites
  }) }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-17T05:35:23.513Z
Answers.LastActivityDate: 2017-11-17T05:48:53.803Z
Answers.LastEditDate: 2017-11-17T05:48:53.803Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Who knows how to loop through all sites in multisite installation? I want to create kind of drop down menu to switch between websites.
  I've found this code inside the backend template but looks like it doesn't works in frontend</p>
  
  <pre><code>{% for site in allSites %}
              &lt;tr data-id="{{ site.id }}" data-name="{{ site.name|t('site') }}"&gt;
                  &lt;th scope="row" data-title="{{ 'Name'|t('app') }}"&gt;&lt;a href="{{ url('settings/sites/' ~ site.id) }}"&gt;{{ site.name|t('site') }}&lt;/a&gt;&lt;/th&gt;
                  &lt;td data-title="{{ 'Handle'|t('app') }}"&gt;&lt;code&gt;{{ site.handle }}&lt;/code&gt;&lt;/td&gt;
                  &lt;td data-title="{{ 'Language'|t('app') }}"&gt;&lt;code&gt;{{ site.language }}&lt;/code&gt;&lt;/td&gt;
                  &lt;td data-title="{{ 'Base URL'|t('app') }}"&gt;&lt;code&gt;{{ site.baseUrl }}&lt;/code&gt;&lt;/td&gt;
                  {% if multiple %}
                      &lt;td class="thin"&gt;&lt;a class="move icon" title="{{ 'Reorder'|t('app') }}" role="button"&gt;&lt;/a&gt;&lt;/td&gt;
                      &lt;td class="thin"&gt;&lt;a class="delete icon" title="{{ 'Delete'|t('app') }}" role="button"&gt;&lt;/a&gt;&lt;/td&gt;
                  {% endif %}
              &lt;/tr&gt;
          {% endfor %}
  </code></pre>
  
  <p>UPD:
  Now I can fetch names of websites by their Ids:</p>
  
  <pre><code>      {% set allSitesIds = craft.app.sites.getAllSiteIds() %}
    {% for site in allSitesIds %}
    {% set siteName = craft.app.sites.getSiteById(site) %}
    {{siteName}}
    {% endfor %}
  </code></pre>
  
Tags: <templating><craft3><multi-site>
Title: Loop through all sites in multisite installation
ThreadText: # Loop through all sites in multisite installation
  
  (Asked by: username_0 on 2017-11-16)
  
  Who knows how to loop through all sites in multisite installation? I want to create kind of drop down menu to switch between websites.
  I've found this code inside the backend template but looks like it doesn't works in frontend
  
  ```
  {% for site in allSites %}
              <tr data-id="{{ site.id }}" data-name="{{ site.name|t('site') }}">
                  <th scope="row" data-title="{{ 'Name'|t('app') }}"><a href="{{ url('settings/sites/' ~ site.id) }}">{{ site.name|t('site') }}</a></th>
                  <td data-title="{{ 'Handle'|t('app') }}"><code>{{ site.handle }}</code></td>
                  <td data-title="{{ 'Language'|t('app') }}"><code>{{ site.language }}</code></td>
                  <td data-title="{{ 'Base URL'|t('app') }}"><code>{{ site.baseUrl }}</code></td>
                  {% if multiple %}
                      <td class="thin"><a class="move icon" title="{{ 'Reorder'|t('app') }}" role="button"></a></td>
                      <td class="thin"><a class="delete icon" title="{{ 'Delete'|t('app') }}" role="button"></a></td>
                  {% endif %}
              </tr>
          {% endfor %}
  
  ```
  
  UPD:
  Now I can fetch names of websites by their Ids:
  
  ```
        {% set allSitesIds = craft.app.sites.getAllSiteIds() %}
    {% for site in allSitesIds %}
    {% set siteName = craft.app.sites.getSiteById(site) %}
    {{siteName}}
    {% endfor %}
  
  ```
  
  **Tags:** templating, craft3, multi-site
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-11-17. Score: 4 | Accepted answer
  
  If you want to do this purely from a template, then something like this:
  
  ```
  {% set sites = craft.app.sites.getAllSites() %}
  
  {% for site in sites %}
      <li>{{ site.name }}</li>
  {% endfor %}
  
  ```
  
  In Craft 3, `craft.app` in a template basically gives you access to everything in the [`services`](https://github.com/craftcms/cms/tree/develop/src/services) namespace. Adding `craft.app.sites` opens up all of the methods in [`services/Sites.php`](https://github.com/craftcms/cms/blob/develop/src/services/Sites.php)
  
  
  ---
  
  ### Answer by username_2 on 2017-11-17. Score: 4
  
  username_0,
  
  As I responded in the slack, I will respond here in case anyone else has this question.
  
  In the template you can access this information like so:
  
  ```
  SiteUrl: {{ siteUrl }}
  Langauge: {{ craft.app.language }}
  Handle: {{ craft.app.sites.currentSite.handle }}
  
  ```
  
  I'm not entirely positive how to loop the sites in the template but you can get a sites object in PHP by running the following in a controller somewhere or in the `init()` method of any plugin that is installed:
  
  ```
  $sites = Craft::$app->getSites();
  var_dump($sites);
  exit;
  
  ```
  
  This will output the structure of the sites object. Baring this you can look through the craft source / docs to find the details on the site object that returns. There should be a method for getting sites by id or by handle; `$sites->getSitesByID()` or `$sites->getSitesByHandle()` but I have not double checked this so you'll have to try it or look it up. If there is not a method then it should be accessible via an array property like `$sites->sitesByHandle` or `$sites->byHandle`. Again you'll have to double check on this.
  
  By the way, that functionality should translate to the template version of the sites object `craft.app.sites` so you should be able to do:
  
  ```
  {% set sites = craft.app.sites.getSitesByHandle() %}
  
  ```
  
  Or something like that anyway.
  
  Once you figure it out feel free to update this to reflect the proper methods / properties :)
  
  
  ---
  
  ### Answer by username_3 on 2017-11-17. Score: 1
  
  I created a script in order to save different credentials for each site in my google analytics plugin.
  So I had to render a dropdown to chose the site as well. Here is my code
  
  First the Plugin variable. You could actually remove the most and just return the `sites` variable
  
  ```
  public function getSites(){
      $response = [];
      $sites = Craft::$app->sites->getAllSites();
      // I had some permission checks here as well to see if the user 
      // has access to the site, but I removed that in this example
      foreach($sites as $site){
          $response[] = array(
              "label" => $site->name,
              "value" => $site->id
          );
      }
      return $response;
  }
  
  ```
  
  And this is my template.
  
  ```
  {% set sites = craft.googleanalytics.getSites() %}
  {% set siteId = entry.lnkSiteId? entry.lnkSiteId : sites[0]["value"] %}
  {{ forms.selectField({
      label:          'Seite'|t,
      id:             'lnkSiteId',
      name:           'lnkSiteId',
      instructions:   'Bitte wählen Sie die Seite'|t,
      value:          siteId,
      options:        sites
  }) }}
  
  ```
  
  
  ---
  
  