テーマコンポーネントで、画面に画像(帽子)を表示する場合についてです。
SCSSで画像を使用するには、アセット名を直接使用します。
これらの画像はabout.jsonで定義されています。
{
"name": "christmas-decorations",
"about_url": "",
"license_url": "",
"author": "",
"assets": {
"red-hat1": "assets/decorations/red-hat1.png",
"red-hat2": "assets/decorations/red-hat2.png",
"squirrel2": "assets/decorations/squirrel2.png"
}
}
これらの画像をSCSSで使用するには、アセット名を直接使用するとうまく機能します。
background: transparent url($red-hat1) top left no-repeat;
画像を選択する設定を追加したいです。
settings.ymlには、画面に表示する帽子の画像を選択するために、次のように記述しています。
hat_type:
default: red-hat1
type: enum
choices:
- red-hat1
- red-hat2
- squirrel2
SCSSでif elseを使用して画像を使用することもできますが、このif elseブロックは長くなりすぎる可能性があります。
@if $hat_type == "red-hat1" {
background: url($red-hat1) no-repeat center;
} @else if $hat_type == "red-hat2" {
background: url($red-hat2) no-repeat center;
} @else if $hat_type == "squirrel2" {
background: url($squirrel2) no-repeat center;
}
SCSSで関数を使用してみました。
background: url(get_image($hat_type)) no-repeat center;
@function get_image($name) {
@return "assets/decorations/" + $name + ".png";
}
しかし、これは次のようなURLを生成します。
https://www.domain.com/stylesheets/assets/decorations/squirrel2.png
これは機能しません。次のように解決される必要があります。
https://www.domain.com/uploads/db9860/original/3X/9/1/91d36fdb030047c6390f9ca85604fdbfd2e3fc12.png
何かヒントをいただけないでしょうか。よろしくお願いします
…


