|
| 1 | +# Tutorial 05 - Customizing the look & feel |
| 2 | + |
| 3 | +In this session we will customize minor visual elements of our Invenio |
| 4 | +instance, like the logo of our instance, the colors and fonts, the way search |
| 5 | +results for records are displayed and the display page for every record. |
| 6 | + |
| 7 | +## Step 1: Run the development server |
| 8 | + |
| 9 | +To run the web development server: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ cd src/my-site |
| 13 | +$ docker-compose up -d |
| 14 | +$ ./scripts/server |
| 15 | +``` |
| 16 | + |
| 17 | +## Step 2: Change the frontpage title |
| 18 | + |
| 19 | +Although the default look of the instance looks fine, we can do better! Let's |
| 20 | +start small by changing some text on the frontpage. To do so we have to edit |
| 21 | +`my_site/config.py`: |
| 22 | + |
| 23 | +```diff |
| 24 | +# Theme configuration |
| 25 | +# =================== |
| 26 | +#: Site name |
| 27 | +THEME_SITENAME = _('My site') |
| 28 | +#: Use default frontpage. |
| 29 | +THEME_FRONTPAGE = True |
| 30 | +#: Frontpage title. |
| 31 | +-THEME_FRONTPAGE_TITLE = _('My site') |
| 32 | ++THEME_FRONTPAGE_TITLE = _('The coolest repository!') |
| 33 | +#: Frontpage template. |
| 34 | +THEME_FRONTPAGE_TEMPLATE = 'my_site/frontpage.html' |
| 35 | +``` |
| 36 | + |
| 37 | +If you now go to <https://localhost:5000/> you will see the changed title: |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## Step 3: Change the site logo |
| 42 | + |
| 43 | +Our instance is powered by Invenio, but not defined by it! Let's change the |
| 44 | +logo to something else. First we have to add the actual image of our new logo |
| 45 | +inside a folder that our application will know it's part of its assets: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Create the "static/images" folder inside the "my_site/theme" directory |
| 49 | +$ mkdir -p my_site/theme/static/images |
| 50 | +# Copy the provided logo from |
| 51 | +$ cp ~/src/training/05-customizing-invenio/extras/my-site-logo.png my_site/theme/static/images/ |
| 52 | +# We have to tell the application to "collect" the new static file |
| 53 | +$ invenio collect -v |
| 54 | +Collect static from blueprints. |
| 55 | +Copied: [my_site] '/home/bootcamp/.local/share/virtualenvs/my-site-7Oi5HgLM/var/instance/static/images/my-site-logo.png' |
| 56 | +``` |
| 57 | + |
| 58 | +We also have to override the `THEME_LOGO` configuration variable, by adding it |
| 59 | +in `my_site/config.py`: |
| 60 | + |
| 61 | +```diff |
| 62 | +# Theme configuration |
| 63 | +# =================== |
| 64 | +#: Site name |
| 65 | +THEME_SITENAME = _('My site') |
| 66 | +#: Use default frontpage. |
| 67 | +THEME_FRONTPAGE = True |
| 68 | +#: Frontpage title. |
| 69 | +THEME_FRONTPAGE_TITLE = _('The coolest repository!') |
| 70 | +#: Frontpage template. |
| 71 | +THEME_FRONTPAGE_TEMPLATE = 'my_site/frontpage.html' |
| 72 | ++#: Theme logo. |
| 73 | ++THEME_LOGO = 'images/my-site-logo.png' |
| 74 | +``` |
| 75 | + |
| 76 | +If you reload the page you will see the new logo on the top left: |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## Step 4: Change the theme color |
| 81 | + |
| 82 | +Until this moment, we've basically performed "content" changes. In order to |
| 83 | +modify the "style" of the site we have to make changes to the produced CSS. |
| 84 | + |
| 85 | +Invenio uses SCSS in order define CSS styles in a flexible and extensible way. |
| 86 | +The `.scss` we are interested in changing is |
| 87 | +`my_site/theme/assets/scss/variables.scss`: |
| 88 | + |
| 89 | +```diff |
| 90 | +@import "../invenio_theme/variables"; |
| 91 | + |
| 92 | +// If you want to change the primary color you can do something like: |
| 93 | +-// $color1: rgba(100, 42, 156, 0.8); |
| 94 | +-// $color1-gradient: lighten($color1, 15%); |
| 95 | +-// $navbar-default-bg: $color1; |
| 96 | ++$color1: rgba(100, 42, 156, 0.8); |
| 97 | ++$color1-gradient: lighten($color1, 15%); |
| 98 | ++$navbar-default-bg: $color1; |
| 99 | +``` |
| 100 | + |
| 101 | +After changing the file, we have to rebuild our assets using the `invenio |
| 102 | +webpack` command: |
| 103 | + |
| 104 | +```bash |
| 105 | +(my-site) $ invenio webpack buildall |
| 106 | +...webpack |
| 107 | +``` |
| 108 | + |
| 109 | +If we reload our page now we should see our brand new design: |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +## Step 5: Change how record search results are displayed |
| 114 | + |
| 115 | +If you navigate to the search results page (<https://localhost:5000/search>) |
| 116 | +you can see the following: |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +Let's change the way the title and authors of each result look like. The |
| 121 | +current search UI application is built with AngularJS and its various |
| 122 | +components are defined via Angular HTML templates. In our case we'll have to |
| 123 | +modify the `my_site/records/static/templates/records/results.html`: |
| 124 | + |
| 125 | +```diff |
| 126 | +<div ng-repeat="record in vm.invenioSearchResults.hits.hits track by $index"> |
| 127 | +- <h4><a target="_self" ng-href="/records/{{ record.id }}">{{ record.metadata.title }}</a></h4> |
| 128 | ++ <h3><a target="_self" ng-href="/records/{{ record.id }}">{{ record.metadata.title }}</a></h3> |
| 129 | ++ <strong>Authors</strong> |
| 130 | + <ul class="list-inline"> |
| 131 | + <li ng-repeat='contributor in record.metadata.contributors'> |
| 132 | +- {{ contributor.name }}; |
| 133 | ++ <em>{{ contributor.name }}</em>; |
| 134 | + </li> |
| 135 | + </ul> |
| 136 | + <hr /> |
| 137 | +</div> |
| 138 | +``` |
| 139 | + |
| 140 | +Again, we'll have to run the `invenio collect` command, since we changes static |
| 141 | +files: |
| 142 | + |
| 143 | +```bash |
| 144 | +(my-site) $ invenio collect -v |
| 145 | +Collect static from blueprints. |
| 146 | +Copied: [my_site_records] '/home/bootcamp/.local/share/virtualenvs/my-site-7Oi5HgLM/var/instance/static/templates/records/results.html' |
| 147 | +``` |
| 148 | + |
| 149 | +And now, if we refresh we'll see that our search results display differently: |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +## Step 6: Change how the record page |
| 154 | + |
| 155 | +If you actually click on one of the search results you will be redirected to |
| 156 | +the record's page: |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +To change this view we'll have to modify the Jinja template that renders the |
| 161 | +page, `my_site/records/templates/records/record.html`. Let's to something |
| 162 | +similar to what we did with the search results: |
| 163 | + |
| 164 | +```diff |
| 165 | +{%- block page_body %} |
| 166 | +<div class="container"> |
| 167 | + <h2>{{record.title}}</h2> |
| 168 | +- <div class="panel panel-default"> |
| 169 | +- <ul class="list-group"> |
| 170 | +- {{ record_content(record) }} |
| 171 | +- </ul> |
| 172 | +- </div> |
| 173 | ++ <strong>Authors</strong> |
| 174 | ++ {% for author in record.contributors %} |
| 175 | ++ <em>{{ author.name }}</em>; |
| 176 | ++ {% endfor %} |
| 177 | +</div> |
| 178 | +{%- endblock %} |
| 179 | +``` |
| 180 | + |
| 181 | +If you refresh the record's page you'll see something like this: |
| 182 | + |
| 183 | + |
0 commit comments