<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>

CC BY-SA 3.0
2017-11-17T18:55:46.913Z
2017-11-17T18:55:46.913Z
Brad Bell
<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>

CC BY-SA 3.0
2017-11-17T02:27:07.690Z
2017-11-17T02:27:07.690Z
Rick Kukiela
<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>

CC BY-SA 3.0
2017-11-17T05:35:23.513Z
2017-11-17T05:48:53.803Z
2017-11-17T05:48:53.803Z
Robin Schambach
<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>

<templating><craft3><multi-site>
Loop through all sites in multisite installation
# 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 %}
            
                {{ site.name|t('site') }}
                {{ site.handle }}
                {{ site.language }}
                {{ site.baseUrl }}
                {% if multiple %}
                    
                    
                {% endif %}
            
        {% 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 %}
    {{ site.name }}
{% endfor %}

In Craft 3, craft.app in a template basically gives you access to everything in the services namespace.  Adding craft.app.sites opens up all of the methods in services/Sites.php


---

### Answer by username_2 on 2017-11-17. Score: 4

Nikolay,
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
}) }}


---

